我正在尝试创建一个批处理文件,根据操作系统的版本执行不同的“choice”命令。在 Windows 7 和 Windows XP 中,“choice”命令的语法不同。
“choice”命令返回1表示Y,2表示N。下面的命令会返回正确的错误级别:
对于 Windows 7:
“choice”命令返回1表示Y,2表示N。下面的命令会返回正确的错误级别:
对于 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操作系统版本的命令结合使用时,在我的Windows XP和Windows 7代码块中,无论在选择命令之前还是之后,errorlevel都会返回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.
)
如您所见,我甚至在执行choice命令之前尝试清除errorlevel变量,但是在执行choice命令后,errorlevel仍然保持为0。
有什么建议吗? 谢谢!