cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Detect Windows Version in a Batch File

dgschaefer
21-Topaz II

Detect Windows Version in a Batch File

Hello,

Not quite Pro/E related, but it does relate to our launching of Pro/E.

I'm the guinea pig here with the first Windows 7 machine and I'm running
into some quirks with our launch batch files. Mainly, the issue is on
of paths, XP paths are different to Windows 7 paths for certain things.

If I could find the windows version in the batch file, then I could
likely write an if-then routine to set certain variables one way for XP
and the other for Windows 7. Anyone know how to do this?

Doug Schaefer

This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.
--
Doug Schaefer | Experienced Mechanical Design Engineer
LinkedIn
5 REPLIES 5

This is almost a month old but it looked like an interesting problem so I had to try it:

Since the OS variable always just returns windows_nt, the ver command is the best place to get the true version. The only way I could find to get command line output into an environment variable is with a for statement:


for /f "delims=" %l in ('ver') do set VERSION=%l


(delims= clears the default delimiters)

Once you have that you can use the VERSION string to make a decision:

if "%VERSION%"=="Microsoft Windows [Version 6.1.7600]" ECHO WIN7


Windows 7 returns 6.1 and XP returns 5.1

I missed the original post. This is one of those interesting questions.

6.1 identifies Windows 7 or Windows Server 2008 R2. The full set of Windows versions are listed on the Microsoft web site.

http://msdn.microsoft.com/en-us/library/ms724832%28VS.85%29.aspx

I believe service packs change the build number (i.e. 6000 for 2008 R2 or 2700 for Windows 7). So depending on your use case you may need to periodically update your script to differentiate between Windows 7 and Windows 7 SP1.

Add this line to the end of the previous example to identify the major version (i.e. Windows XP or Windows 7) regardless of the installed service packs.

set VERSION=%VERSION:~27,3%

Test with:

echo %VERSION%

6.1

Taking another approach, you can extract it from the registry.

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion

  • CurrentBuildNumber: 2700
  • CurrentVersion: 6.1
  • ProductName: Windows 7 Ultimate

Use this syntax to read registry values from the command line.
reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion
CurrentVersion REG_SZ 6.1


This approach works for determining the Internet Explorer version too...
reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer" /v Version

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
Version REG_SZ 8.0.7600.16385

OK, I took this ideaand ran with it... 🙂

Here is my batch script:

----

@echo off
::Original Code Source - http://malektips.com/xp_dos_0025p.html
::Modified by Mark A. Kelley

set _SCRATCH-FILE_=%TMP%\__Windoze-Version-Scratch___.txt
systeminfo>%_SCRATCH-FILE_%

type %_SCRATCH-FILE_% | find "2003" > nul
if %ERRORLEVEL% == 0 goto :ver_2003

type %_SCRATCH-FILE_% | find "XP" > nul
if %ERRORLEVEL% == 0 goto :ver_xp

type %_SCRATCH-FILE_% | find "2000" > nul
if %ERRORLEVEL% == 0 goto :ver_2000

type %_SCRATCH-FILE_% | find "NT" > nul
if %ERRORLEVEL% == 0 goto :ver_nt

if not exist %SystemRoot%\system32\systeminfo.exe goto :warnthenexit

systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i

echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto :ver_7

echo %vers% | find "Windows Server 2008" > nul
if %ERRORLEVEL% == 0 goto :ver_2008

echo %vers% | find "Windows Vista" > nul
if %ERRORLEVEL% == 0 goto :ver_vista

goto :warnthenexit

:ver_7
:Run Windows 7 specific commands here.
echo Windows 7
goto exit

:ver_2008
:Run Windows Server 2008 specific commands here.
echo Windows Server 2008
goto exit

:ver_vista
:Run Windows Vista specific commands here.
echo Windows Vista
goto exit

:ver_2003
:Run Windows Server 2003 specific commands here.
echo Windows Server 2003
goto exit

:ver_xp
:Run Windows XP specific commands here.
echo Windows XP
goto exit

:ver_2000
:Run Windows 2000 specific commands here.
echo Windows 2000
goto exit

:ver_nt
:Run Windows NT specific commands here.
echo Windows NT
goto exit

:warnthenexit
echo Machine undetermined.

:exit
if exist %_SCRATCH-FILE_% del %_SCRATCH-FILE_%>nul

---

This works on XP & Win 2003 Server, I don't have any other OS to test it on, so Caveat Emptor! 🙂

I'm curious why you altered it. What improvements were you after?

Doug Schaefer
--
Doug Schaefer | Experienced Mechanical Design Engineer
LinkedIn

The original script returned "Machine undetermined." on my Win 2003 Server.

I fixed this by parsing the output of "systeminfo" instead of "ver"

And while we're at it, Here's another way of doing it:

---

@echo off

for /f "tokens=1-5* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion ^| find /i "REG_SZ") do set WINDOZE_VERSION=%%c
for /f "tokens=1-5* delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion" /v CurrentBuildNumber ^| find /i "REG_SZ") do set WINDOZE_BUILD=%%c

::Source for version info: http://msdn.microsoft.com/en-us/library/ms724832(VS.85).aspx?ppud=4
set WINDOZE_VERSION_NAME=Unknown
if {"%WINDOZE_VERSION%"}=={"4.0"} set WINDOZE_VERSION_NAME=Windows NT?
if {"%WINDOZE_VERSION%"}=={"5.0"} set WINDOZE_VERSION_NAME=Windows 2000
if {"%WINDOZE_VERSION%"}=={"5.1"} set WINDOZE_VERSION_NAME=Windows XP
if {"%WINDOZE_VERSION%"}=={"5.2"} set WINDOZE_VERSION_NAME=Windows XP 64-Bit Edition or Windows Server 2003 or Windows Server 2003 R2
if {"%WINDOZE_VERSION%"}=={"6.0"} set WINDOZE_VERSION_NAME=Windows Vista or Windows Server 2008
if {"%WINDOZE_VERSION%"}=={"6.1"} set WINDOZE_VERSION_NAME=Windows 7 or Windows Server 2008 R2

echo Windoze Version=%WINDOZE_VERSION%, Build %WINDOZE_BUILD% (%WINDOZE_VERSION_NAME%)

---

I don't like this one as well, because it is far less precise in determining the OS version...

Top Tags