简体   繁体   中英

Detect windows 2003 and XP using batch file

Need to write a batch file which should run different commands depending on windows version. Trying to use the following batch, it doesn't work

@ECHO off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

FOR /f "tokens=1,2* delims=." %%a IN ('ver') DO (

SET WVer=%%a
SET WVer=!WVer:~-1!
SET WVer=!WVer!.%%b.%%c
SET WVer=!WVer:]=!
)

ECHO %WVer%

set result=false
if %WVer%==5.1.2600 set result=true
if %WVer%==5.2.3790 set result=true
if "%result%" == "true" (
    GOTO  XPor2003
)
if "%result%" == "false" (
 GOTO  nope
)


:XPor2003
echo XPor2003

:nope
echo nope

copy of my comments: Here is a result of my batch script on a Windows XP:

5.2.3790 x64 
XPor2003
nope

I'm just wondering why nope result is shown. I'm trying to figure out why my batch script transfers control to incorrect label

try this:

@echo off
setlocal EnableDelayedExpansion

::Identify OS
for /F "delims=" %%a in ('ver') do set ver=%%a
set Version=
for %%a in (95=95 98=98 ME=ME NT=NT 2000=2000 5.1.=XP 5.2.=2003 6.0.=Vista 6.1.=7 6.2.=8) do (
    if "!Version!" equ "this" (
        set Version=Windows %%a
    ) else if "!ver: %%a=!" neq "%ver%" (
        set Version=this
    )
)

::Identify bit
if exist "%SYSTEMDRIVE%\Program Files (x86)" (
    set Type=64 bit
) else (
    set Type=32 bit
)

::Display result
echo %Version% %Type%
goto :eof


::Goto right version
goto %Version: =_%


:Windows_8
echo Windows 8

:Windows_7
echo Windows 7

© Aacini

Note the three goto :EOF commands which help with the program flow.

set result=false
if %WVer%==5.1.2600 set result=true
if %WVer%==5.2.3790 set result=true
if "%result%" == "true" (
    GOTO  XPor2003
)
if "%result%" == "false" (
 GOTO  nope
)
goto :EOF


:XPor2003
echo XPor2003
goto :EOF

:nope
echo nope
goto :EOF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM