我想創建一個批處理文件,該文件根據正在執行的Windows版本執行不同的'選擇'命令。 Windows 7和Windows XP的選擇命令語法不同。批處理文件'選擇'命令的錯誤級別返回0
選擇命令返回用於Y 1和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代碼塊中的選擇命令之後的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.
)
正如你所看到的,我甚至嘗試執行選擇命令之前清除錯誤級別變種,但在執行選擇命令後,錯誤級別仍爲0。
任何提示? 謝謝!