Reputation: 1405
A program returns a version string of this regex type [0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}
on the command line. Therefore the output looks like this:
C:\users\user\desktop>a.exe --version
Version: 1.22.53.4134
I save the output to a variable Version
by this get-version-info.bat
for /f "usebackq tokens=1* delims=: " %%i in (`a.exe --version`) do (
if /i "%%i"=="Version" set "Version=%%j"
)
To solve my exact problem I only need the major and minor release number(s), but a general, adaptable solution would be also good of course.
How to get major and minor version number of the program in batch file?
Upvotes: 1
Views: 496
Reputation: 49096
You can use this single command line in your batch file:
for /F "tokens=1-3 delims=:. " %%I in ('a.exe --version') do if /I "%%I" == "Version" set "Version=%%J.%%K"
The delimiters for splitting up the line output by a.exe
are colon :
, point .
and space
as specified with delims=:.
.
Of interest are only the first 3 substrings (tokens) as specified with tokens=1-3
. The first substring being Version
is assigned to specified loop variable I
. The second substring being 1
is assigned to next loop variable according to ASCII table which is J
. and the third substring being 22
is assigned to loop variable K
.
Upvotes: 1
Reputation:
Add the dot to the delims, extend the tokens and compound your Ver with the wanted elements:
for /f "usebackq tokens=1-4* delims=:. " %%i in (`a.exe --version`) do (
if /i "%%i"=="Version" set "VerMAjorMinor=%%j.%%k"
)
Upvotes: 1