Search code examples
windowsbatch-filecommand-linecmd

How to extract version number in a Windows batch file?


I need to extract the Major , Minor and Revision numbers from a string and to achieve this I'm trying to split a string in a batch file using a '.' character as the delimiter.

For ex: If the user enters 1.0.2 in the command prompt I should be able to extract

  • 1 - Major version,
  • 0 - Minor version and
  • 2 - Revision

I'm trying to use the FOR command to achieve this, but just not getting through. Can anyone help me out with the extracting part

@ECHO OFF & SETLOCAL 
set /p "ReleaseVersion=Please specify the software release version : "

:nextVar
for /F "tokens=1* delims=." %%a in ("%ReleaseVersion%") do (
   set %%a
   set ReleaseVersion=%%b
)
if defined ReleaseVersion goto nextVar

@PAUSE

Solution

  • Here you go...

    @ECHO OFF & SETLOCAL 
    set /p "ReleaseVersion=Please specify the software release version : "
    
    for /F "tokens=1,2,3 delims=." %%a in ("%ReleaseVersion%") do (
       set Major=%%a
       set Minor=%%b
       set Revision=%%c
    )
    
    echo Major: %Major%, Minor: %Minor%, Revision: %Revision%
    

    Input/output:

    Please specify the software release version : 1.0.2

    Major: 1, Minor: 0, Revision: 2