Extract version from XML with Windows Batch - windows

I have a XML file generated by Visual Studio that contains the version of the product, hence the tag will always be there just the value changes.
I need to extract the version with windows command line somehow. This is the tag in the file and I need only the version number "1.0.0.0":
<?define BuildVersion = 1.0.0.0 ?>
Is there a built in tool (maybe with findstr) that can accomplish this?

Use for /F loop to parse the output of findstr and extract the text you need (using space as delimiter, get the 4th token):
for /F "tokens=4 delims= " %%E in ('
findstr "BuildVersion" "file.xml"
') do #echo %%E
Use a single % instead of two when running from the command line though.
For a more comprehensive information please check this post.

Related

How to run an action/command based on a result of a previous command

I have a deployment that installs a driver and I want to provide the ability to uninstall.
Im leveraging the PNPUTIL.exe tool.
I know the syntax to delete and uninstall the driver, ex:
pnputil.exe /delete-driver oem103.inf /uninstall /force
But my issue, is the oem*.inf number designation is random on each machine, so I can't hard code the .inf into the command and call it a day.
pnputil has /enum-driver switch that will give you details of all the drivers in the DriverStore. Among the line items is the original name of .inf (something I can work with) and the oem# associated with it.
So what I need help with is scripting something that will enumerate the drivers pipe the results to the command to be able the run /delete-drive and /uninstall switches
I tried messing with the Find and FindSTR commands, but it only returned the one line which was the name of the original .inf. I need the OEM# associated with original name of the .inf to be piped to the command.
In the output of pnputil, the desired oemXX.inf is one line above the Original Name.
So numerate the output, look for the original name and subtract one from the line number. This is the line number where you find the oemXX.inf.
Then find that line and extract the oemXX string. (the for %%b is to get rid of the leading spaces)
#echo off
setlocal
set "Orig=rt640x64.inf"
pnputil /enum-drivers |findstr /n "^" > pnputil.txt
for /f "delims=:" %%a in ('findstr /c:" %Orig%" pnputil.txt') do set /a line=%%a-1
for /f "tokens=3 delims=:" %%a in ('findstr /b "%line%:" pnputil.txt') do for %%b in (%%a) do set "oem=%%b"
echo "%oem%"
Note: the output of pnputil is language-dependent, but this code doesn't look for words (except the "Original name" of course) but for line numbers, so it should work on all languages.

Windows Batch File for getting file name and version on a folder

Experts,
Requirement is to get all the Filename and FileVersion in the directory path
Example:
Directory_Path = C:\Program Files (x86)\Oracle\Document Capture
When I run the batch it should ideally list out all the filenames and associated fileversion of the file (shouldnt consider any file without version or shouldnt consider any folder)
My Expected Output:
Filename Fileversion
-------- -----------
vabc.dll 1.3
vabd.dll 1.3
vace.dll 1.4
I found this to work on our Windows 2008 R2
wmic datafile where name="C:\\Windows\\System32\\msiexec.exe" get Version /value
Source= Windows: Command line to read version info of an executable file?
But i couldnt figure a way out to pass each of the filename into this command automatically and get me the output in a text file.
Any help is appreciated
The simplest solution is to specify the drive and path in the WHERE clause, and let WMIC discover the file names. You can also specify VERSION IS NOT NULL in the where clause.
The listed FileName will be the base file name only, without the extension. The extension is available in the Extension column (fancy that!).
wmic datafile where "drive='c:' and path='\\Program Files (x86)\\Oracle\\Document Capture\\' and version is not null" get FileName, Extension, Version
Columns are always listed in alphabetical order (by name), regardless what order you requested. So you should get output like the following:
Extension FileName Version
dll vabc 1.3
dll vabd 1.3
dll vace 1.3
Or you can request the Name column instead of FileName and Extension, but that will include the complete path.
Name Version
C:\Program Files (x86)\Oracle\Document Capture\vabc.dll 1.3
C:\Program Files (x86)\Oracle\Document Capture\vabd.dll 1.3
C:\Program Files (x86)\Oracle\Document Capture\vace.dll 1.4
The other option is to use a FOR loop to iterate all the files, and then pass the full path of each file in a separate WMIC call that is parsed by a FOR /F. There is an unfortunate "feature" (aka bug) with how FOR /F converts WMIC unicode output into ansi, such that all lines have an unwanted trailing carriage return (\r) that can lead to various problems. The unwanted trailing \r is eliminated by an extra FOR /F statement.
The VERSION IS NOT NULL clause is not needed because the pair of FOR /F statements will strip out lines that contain nothing but spaces.
#echo off
set "loc=C:\Program Files (x86)\Oracle\Document Capture\"
pushd "%loc%"
for %%F in (*) do for /f "skip=1 tokens=*" %%A in (
'wmic datafile where "name='%loc:\=\\%%%F'" get version 2^>nul'
) do for /f "delims=" %%V in ("%%A") do echo %%F %%V
popd
Output should then look like the following:
vabc.dll 1.3
vabd.dll 1.3
vace.dll 1.4

Get file name and append to beginning of line

I'm trying to get a side-by-side file path and file name in a text file so I can make inserting into a database easier. I've taken a look at other examples around SO, but I haven't been able to understand what is going on. For instance, I saw this batch file to append file names to end of lines but figured that I shouldn't ask for clarification because it's 1.5 years old.
What I have is a text file of file paths. They look like this:
\\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
What I want it to look like is this:
1AD0019.tif \\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
so that I can insert it into a database. Is there an easy way to do this on Windows via Batch files?
No batch file required. From the command line:
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do #echo %~nxF %~dpF)
But that output format is risky because file and folder names can contain spaces, so it may be difficult to determine where the file name ends and the path begins. Better to enclose the file and path within quotes.
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do echo "%~nxF" "%~dpF")
if done within a batch file, then percents must be doubled.
#echo off
>"outputFile.txt" (
for /f "usebackq eol=: delims=" %%F in ("inputFile.txt") do echo "%%~nxF" "%%~dpF"
)
You should read the built in help for the FOR command. Type help for or for /? from a command prompt to get help. That strategy works for pretty much for all commands.
In powershell, this little script should do the trick. In the first line, just specify the name of the text file that contains all the file paths.
$filelist="c:\temp\filelist.txt"
foreach($L in Get-Content $filelist) {
$i = $L.length - $L.lastindexof('\') -1
$fname=$L.substring($L.length - $i, $i)
echo ($fname + ' ' + $L)
}
If you don't have powershell installed on your machine, check out http://technet.microsoft.com/en-us/library/hh847837.aspx.
#ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%i IN (yourfile.txt) DO ECHO %%~nxi %%i
)>newfile.txt
GOTO :EOF
No big drama - all on one active line, but spaced for clarity

Getting product version from file via cmd/bat

I have a file with Version resource that File vesrion/Product version fields are filled. I need to retrieve Product version via BAT file. Example, I have File with ProductVersion 1.0.1 in the output of bat file I wan't to have string "101" or "1.0.1"
You can use sigcheck tool which is part of Sysinternals Suite since filever is quiet old, e.g.
$ sigcheck.exe -q -n app.exe
5.0.0.1241
By specifying -q (quiet) and -n, it'll show you only the file version number.
How to use the Filever.exe tool to obtain specific information about a file in Windows
From what I gather about filever's output it's always in columns and you want the fifth column (version). So a simple for should suffice:
for /f "tokens=5 delims= " %%v in ('filever myFile.dll /b') do echo %%v
For dummy's like me there is one correction in the above statement to get the value of product version it would be like:
for /f "tokens=5 delims= " %%v in ('filever myFile.dll /b /v') do echo %%v
the /v parameter was missing and I am unable to get the correct value.
To read version from resource RC-file you can use:
for /F "tokens=3" %%a in ( 'findstr ProductVersion YourProgram.rc' ) do set VERSION=%%~a

Need help with a .bat script for parsing W3C logs

Im trying to get a value (IP address) from a W3C logfile (kinda like a text file). This is what I have so far but with no luck:
Set filename=ex%date:~-2,4%%date:~-10,2%%date:~-7,2%.log
For /F "tokens=2 delims=: . " %%A in ('E:\WINDOWS\system32\LogFiles\MSFTPSVC6141885\%filename%') do (Set ip=%%A)
and the log file looks like:
# Software: Microsoft Internet Information Services 6.0
# Version: 1.0
#Date: 2009-01-10 20:58:16
#Fields: time c-ip cs-method cs-uri-stem sc-status sc-win32-status
#20:58:16 10.10.1.111 [25]USER anonymous 331 0
so the IP adress is on the 5th line second column (10.10.1.111)
any feedback would be appreciated!
Have you tried Microsoft Log Parser? Supposedly it supports W3C-style log files out-of-the-box. I don't know what you're trying to do, but it might be easier than hand-crafting a batch file.
Alternatively, install AWK (e.g. from Cygwin). Or even Perl -- this is its raison-d'etre.
Change your for line to this:
For /F "skip=4 tokens=2" %%A in (E:\WINDOWS\system32\LogFiles\MSFTPSVC6141885\%filename%) do (
Set ip=%%A
goto :DONE
)
:DONE
#echo IP = %ip%
:: Continue script
skip=4 will ignore the first four lines of the log file and start parsing the 5th.
You need the goto to stop from parsing the rest of the lines in the file, otherwise you'll spin through the whole file and ip would equal the second token of the last line, which may or may not be the IP address.
The default delimiter is space, so you don't need to change this with a delims arg. You do want just the second token, which is the IP address.
You don't need to enclose the filename in single quotes, since you're parsing the contents of the file, not the filename string. If the filename has embedded spaces you would have to use this for line instead:
For /F "usebackq skip=4 tokens=2" %%A in ("E:\WINDOWS\system32\LogFiles\MSFTPSVC6141885\%filename%") do (

Resources