0

im running a command to get the version of a program.

FOR /F "tokens=* USEBACKQ" %%F IN (`"%programpath%\bin\program" version`) DO (
set version_full=%%F
)

the result:

Program Final Type 6.6.8 (build v3425fw2f24)

The Program Final Type part can vary. There are different types of this program. Hence why I need to use regex to find the version.

I need to get just the 6.6.8 out of this string and set it to a variable called "version_num". But I actually want it to be 6.6.8.0 if its only three numbers.

Sometimes the version can be 4 digits... like 6.6.8.2. I need to get all 4 numbers if 4 .. or if 3 make it 4 by adding a 0 at the end.

I tried

findstr /R "[0-9].[0.9].[0.9]" %version_full%

But i just get the entire string separated I think byspaces in an error like this:

FINDSTR: Cannot open 6.6.8

So its obviously not finding it.

EDIT:

I've gotten this to work to tell me if its found or not found, but I still cant get the actual found value into a variable.

@echo off
set var1=Program One New 6.6.8 (build ee27a8rr43e)
set "regexp=.*[0-9].[0-9].[0-9].*"
echo %var1%
echo %regexp%
echo %var1%|findstr /r /C:"%regexp%" >nul 2>&1 && echo Found || echo Not found.

Found
4
  • FINDSTR outputs the whole matched string.
    – Akina
    Oct 1, 2019 at 11:59
  • i dont think that helps me.. Oct 1, 2019 at 19:24
  • it does only give you the whole line, but you are getting an error because it is trying to open a file. You would want to echo through a pipe to findstr: echo %version_full%|findstr [0-9].[0.9].[0.9]. Check out: stackoverflow.com/questions/5491383/…
    – Yorik
    Oct 1, 2019 at 20:26
  • yea.. ive gotten this far" echo.%var1%|findstr /C:"%regexp%" >nul 2>&1 && echo Found || echo Not found. " which tells me if its found but i donno how to get WHAT it found into a damn variable. arg. Oct 1, 2019 at 21:22

2 Answers 2

0

I just wrote two separate functions... for the two variations of the product I know will occur. In those functions i just count the amount of characters delim by spaces and pull the number out.

Not good... since if the program changes its name... or version naming convention it breaks. And anytime a new variation is added, I need to account for it. But for now it will work. Cant believe batch doesn't support regex extracts though. Legit surprised.

1
0

I know I am beating a dead thread back to life, but I had some similar requirements and accomplished it finally with a batch file as here. I also put the additional check for the four pieces of version numbers. It can be modified to check for 1 piece or 2 pieces available and add additional zeroes.

@echo off
set str=Program Final Type 6.7.8 (build v3425fw2f24)
REM set str=Program Final Type 6.7.8.9 (build v3425fw2f24)
echo Original: %str%
set str=%str:(=x%
set str=%str:)=x%
echo Replaced(): %str%
set final_str=
for %%a in (%str%) do (
    echo %%a | FindStr /R "[0-9]*\.[0-9]*\."  > nul 2>&1 && set final_str=%%a
)
echo Initial Version: %final_str%
for /F "tokens=1-4 delims=." %%b in ("%final_str%") DO if [%%e] EQU [] set final_str=%final_str%.0
echo Final Version: %final_str%

This gives output as:

Original: Program Final Type 6.7.8 (build v3425fw2f24)
Replaced(): Program Final Type 6.7.8 xbuild v3425fw2f24x
Initial Version: 6.7.8
Final Version: 6.7.8.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.