【发布时间】:2012-01-26 11:47:33
【问题描述】:
我正在尝试创建一个批处理文件,该批处理文件根据正在执行的 Windows 版本执行不同的“选择”命令。选择命令的语法在 Windows 7 和 Windows XP 之间有所不同。
Choice 命令为 Y 返回 1,为 N 返回 2。以下命令返回正确的错误级别:
Windows 7:
choice /t 5 /d Y /m "Do you want to automatically shutdown the computer afterwards "
echo %errorlevel%
if '%errorlevel%'=='1' set Shutdown=T
if '%errorlevel%'=='2' set Shutdown=F
Windows XP:
choice /t:Y,5 "Do you want to automatically shutdown the computer afterwards "
echo %ERRORLEVEL%
if '%ERRORLEVEL%'=='1' set Shutdown=T
if '%ERRORLEVEL%'=='2' set Shutdown=F
但是,当它与检测 Windows 操作系统版本的命令结合使用时,errorlevel 在我的 Windows XP 和 Windows 7 代码块中的选择命令之后的 AN 之前返回 0。
REM Windows XP
ver | findstr /i "5\.1\." > nul
if '%errorlevel%'=='0' (
set errorlevel=''
echo %errorlevel%
choice /t:Y,5 "Do you want to automatically shutdown the computer afterwards "
echo %ERRORLEVEL%
if '%ERRORLEVEL%'=='1' set Shutdown=T
if '%ERRORLEVEL%'=='2' set Shutdown=F
echo.
)
REM Windows 7
ver | findstr /i "6\.1\." > nul
if '%errorlevel%'=='0' (
set errorlevel=''
echo %errorlevel%
choice /t 5 /d Y /m "Do you want to automatically shutdown the computer afterwards "
echo %errorlevel%
if '%errorlevel%'=='1' set Shutdown=T
if '%errorlevel%'=='2' set Shutdown=F
echo.
)
如你所见,我什至尝试在执行选择命令之前清除errorlevel var,但在执行选择命令后errorlevel 仍然为0。
有什么建议吗? 谢谢!
【问题讨论】:
标签: file batch-file command choice errorlevel