2013-08-06 25 views
1

需要编写一个应根据Windows版本运行不同命令的批处理文件。尝试使用下面的批处理,它不工作的我的意见使用批处理文件检测Windows 2003和XP

@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 

副本:这是在Windows XP我的批处理脚本的结果:

5.2.3790 x64 
XPor2003 
nope 

我只是想知道为什么显示nope结果。我试图找出为什么我的批处理脚本将控制权转移到不正确的标签

回答

1

注意三个goto :EOF命令与程序帮助流。

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 
5

试试这个:

@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

+0

下面是在Windows XP我的批处理脚本的结果: '5.2.3790 64 XPor2003 nope' 我只是想知道为什么'显示nope'结果。 –

+0

我在我的XP系统上获得'Windows XP 32 bit',在我的win8系统上获得'Windows 8 64 bit'。 – Endoro

+1

我想弄清楚为什么我的批处理脚本将控件转换为不正确的标签 –