The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,402 other followers

Batch file to detect Windows version number

Posted by jpluimers on 2012/02/08

Most Batch files for detecting Windows versions try to parse the either the [WayBack] output fromVER  or the [WayBackoutput from SYSTEMINFO [WayBack], but forget that there many Windows installations are not English. Some even use WMIC, but [WayBackWMIC is only available for administrators and not available some flavours like XP Home.

Languages issues are always important to watch for. The Dutch Windows XP returns Microsoft Windows XP [versie 5.1.2600] which is just one word different from the English Microsoft Windows XP [Version 5.1.2600]. Other languages may differ even more.

This batch file tries to circumvent the language differences, uses VER and works at least with Dutch and English Windows versions of XP and 7, most likely with many other languages and versions as well.

On a Windows XP SP3 machine, it lists WindowsVersion=5.1.2600 and on a Windows 7 SP1 machine it lists WindowsVersion=6.1.7601.

One possible addition would be to [WayBack] detect x64 or x86.

The detection assumes that VER will emit the version in [angle] brackets, and uses two batch file for loops to get the text in between them using the [WayBacktokens and delims for loop parameters in the first for loop right behind the begin label and the second for loop right after the parse1 label.

Then it splits the remaining text using spaces at the parse2 label, and takes the right most portion using the [WayBackshift command at the parse3 label.

Many thanks to [WayBackRob van der Woude for a lot of interesting batch file documentation. The [WayBackWikipedia page on VER lists the Windows versions that you can see. This is a summary starting at Windows 2000 [the first using angle brackets]:

  • Windows 2000: Microsoft Windows 2000 [Version 5.00.2195]
  • Windows XP: Microsoft Windows XP [Version 5.1.2600]
  • Windows XP 64bit: Microsoft Windows [Version 5.2.3790]
  • Windows Server 2003: Microsoft Windows [Version 5.2.3790]
  • Windows Vista: Microsoft Windows [Version 6.0.6001]
  • Windows Server 2008: Microsoft Windows [Version 6.0.6002]
  • Windows Server 2008 R2: Microsoft Windows [Version 6.1.7600]
  • Windows 7: Microsoft Windows [Version 6.1.7600]
  • Windows 7 SP1: Microsoft Windows [Version 6.1.7601]
  • Windows 8.1: Microsoft Windows [Version 6.3.9600]
  • Windows 10 Build 1803: Microsoft Windows [Version 10.0.17134.165]

And here is the batch file:

@echo off
:begin
  :: get everything from "ver" starting after the first [
  setlocal enableextensions
    for /f "tokens=2 delims=[" %%a in ('ver') do (call :parse1 %%a)
    echo WindowsVersion=%WindowsVersion%
  endlocal
goto :end

:parse1
  :: strip everything from the last [
  ::DEBUG echo %*
  for /f "tokens=1 delims=]" %%a in ("%*") do (call :parse2 %%a)
goto :end

:parse2
  :: get all
  ::DEBUG echo %*
  for /f "tokens=* delims= " %%a in ("%*") do (call :parse3 %%a)
goto :end

:parse3
  ::DEBUG echo %*
  set WindowsVersion=%1
  shift
  if !!==!%1! goto :WindowsVersion
  goto :parse3
:WindowsVersion
  ::DEBUG echo WindowsVersion=%WindowsVersion%
goto :end

:end

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: