none
Need help with batch file to detect Windows Version

    Question

  • I need some help with a batch file, this will be part of a larger batch script. 

     

    I need to declare variables base on what version of Windows is running.  So I queried the ProductName Registry key to get and had it return the Value, that part works fine.  However when I compare the values and issue a goto command it does not work.  All it does is run each IF line, then Runs the function :Windows7Var and then jumps to exit.  So I have something wrong with my Syntax for the if Statements.

     

    @echo off
    
    FOR /F "tokens=2*" %%A IN ('REG.EXE QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /V "ProductName" 2^>NUL ^| Find "REG_SZ"') Do Set RegValuedata=%%B 
    
    echo %RegValueData%
    
    If "%RegValueData%" EQU "Windows 7 Professional" ( goto Windows7Var )
    
    If "%RegValueData%" EQU "Windows Vista (TM) Buisness" ( goto WindowsVistaVar)
    
    If "%RegValueData%" EQU "Windows XP Professional" ( goto WindowsXPVar )
    
    	
    :Windows7Var
    Echo This PC is running Windows 7 Professional
    goto Exit
    
    :WindowsVistaVar
    Echo This PC is running Windows Vista Buisness
    goto Exit
    
    :WindowsXPVar
    echo This PC is running Windows XP Professional
    goto exit
    
    :Exit
    Pause
    Exit

    Monday, March 14, 2011 9:24 PM

Answers

  • echo "%RegValueData%"
    "Windows 7 Enterprise "

    if "%RegValueData%" EQU "Windows 7 Enterprise " echo Works!
    Works!

    There is trailing space there (for windows 7 at least). All if's fail, and you don't tell script to do anything if that happends. So it goes to next line. To avoid it add goto :EOF after if's and change your condition to include space? :)

    HTH
    Bartek

    • Marked as answer by Evers_mark Tuesday, March 15, 2011 12:54 PM
    Monday, March 14, 2011 9:44 PM
    Moderator

All replies

  • Hi,

    The easiest way that I can suggest, if you can use freeware tools, is to use my OSTest utility: http://www.westmesatech.com/wast.html

    Using OSTest instead, you could write:

    ostest -p NT -v 5.1 -r workstation
    if errorlevel 1 goto :XP
    ostest -p NT -v 6 -r workstation
    if errorlevel 1 goto :VISTA
    ostest -p NT -v 7 -r workstation
    if errorlevel 1 goto :WIN7
    ...
    

    Alternatively, you can use -g (a "greater-than" test) to test for a minimum version:

    ostest -p NT -g -v 6 -r workstation
    if errorlevel 1 goto :VISTAORNEWER
    ...
    

    For the curious, -p refers to platform (9x or NT) and -r is role (workstation, server, dc, notworkstation). It can also test architecture (-a x86, -a x64, -a IA64).

    HTH,

    Bill

    Monday, March 14, 2011 9:44 PM
    Moderator
  • echo "%RegValueData%"
    "Windows 7 Enterprise "

    if "%RegValueData%" EQU "Windows 7 Enterprise " echo Works!
    Works!

    There is trailing space there (for windows 7 at least). All if's fail, and you don't tell script to do anything if that happends. So it goes to next line. To avoid it add goto :EOF after if's and change your condition to include space? :)

    HTH
    Bartek

    • Marked as answer by Evers_mark Tuesday, March 15, 2011 12:54 PM
    Monday, March 14, 2011 9:44 PM
    Moderator
  • First on my XP Pro equipped machine the result for RegValueData is "Microsoft Windows XP ", not "Windows XP Professional".  Second, the Vista test has a misspelling in it; it should be Business, not Buisness.  I would imagine there might be other problems with the string matching besides the ones I observed.  I don't have a way to test them all.  In particular a trailing space in the returned result could cause the test to fail. Therefore, I offer that a FIND test might be more robust, something like this ...

    @echo off

    FOR /F "delims=[" %%A IN ('ver') Do Set RegValuedata=%%A

    echo %RegValueData%

    echo "%RegValueData%" | find "Windows 7 Pro" > nul && goto Windows7Var

    echo "%RegValueData%" | find "Vista (TM) Business" > nul && goto WindowsVistaVar

    echo "%RegValueData%" | find "Microsoft Windows XP" > nul && goto :WindowsXPVar

    echo Error
    goto :EOF
       
    :Windows7Var
    Echo This PC is running Windows 7 Professional
    goto exit

    :WindowsVistaVar
    Echo This PC is running Windows Vista Business
    goto exit

    :WindowsXPVar
    echo This PC is running Windows XP Professional
    goto exit

    :Exit
    pause

    BTW, I also change to the simpler VER statement to get the current machine's version.  I suspect it returns the same result as the REG query.  It does for an XP machine.


    Tom Lavedas
    Monday, March 14, 2011 9:51 PM
    Moderator
  • Thanks all...Bartek..that worked, apparently there is a trailing space on each of them.

    I didn't even notice the XP name being incorrect, or the spelling of business.

    Tuesday, March 15, 2011 12:57 PM