Batch Variable SubString Example – Extract Windows Version


In last post, we show you the way to extract substring in batch variable. Now let us review this by another example.

If you type in ver, you will get the version string at windows command line prompt.

D:\>ver

Microsoft Windows [Version 6.3.9600]

Now, if we use for to split the string into four substrings (only extract the last part, which is 6.3.9600] in this case) and we can get each number as we want.

D:\>type ver.bat
@ECHO OFF

FOR /F "tokens=1,2,3,4" %%I IN ('VER') DO (
    SET Ver_Temp=%%L
)

SET Ver_Major=%Ver_Temp:~0,1%
SET Ver_Minor=%Ver_Temp:~2,1%
SET Ver_Build=%Ver_Temp:~-5,4%

ECHO Windows Version:
ECHO   Major %Ver_Major%
ECHO   Major %Ver_Minor%
ECHO   Build %Ver_Build%

The output is something like this:

D:\>ver.bat
Windows Version:
  Major 6
  Major 3
  Build 9600

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
212 words
Last Post: How to get Time using Batch Command - Substring
Next Post: Run as Administrator in Powershell

The Permanent URL is: Batch Variable SubString Example – Extract Windows Version

Leave a Reply