批处理文件“选择"命令的错误级别返回 0 [英] Batch file 'choice' command's errorlevel returns 0

查看:13
本文介绍了批处理文件“选择"命令的错误级别返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个批处理文件,该批处理文件根据正在执行的 Windows 版本执行不同的选择"命令.选择命令的语法在 Windows 7 和 Windows XP 之间有所不同.

I'm trying to create a batch file that performs different 'choice' command based on the version of Windows being executed on. The choice command's syntax is different between Windows 7 and Windows XP.

Choice 命令为 Y 返回 1,为 N 返回 2.以下命令返回正确的错误级别:

Choice command returns a 1 for Y and 2 for N. The following command returns the correct error level:

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:

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.

However, when it is combined with a command to detect the Windows OS version, errorlevel returns 0 before AN after the choice command in both my Windows XP and Windows 7 blocks of code.

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.

As you can see, I even tried clearing the errorlevel var before executing the choice command, but errorlevel remains at 0 after the choice command is executed.

有什么建议吗?谢谢!

推荐答案

您遇到了一个经典问题 - 您试图在带括号的代码块中展开 %errorlevel%.这种形式的扩展发生在解析时,但是整个 IF 构造被一次解析,所以 %errorlevel% 的值不会改变.

You have run across a classic problem - You are attempting to expand %errorlevel% within a parenthesized block of code. That form of expansion occurs at parse time, but the entire IF construct is parsed at once, so the value of %errorlevel% will not change.

解决方案很简单 - 延迟扩展.您需要 SETLOCAL EnableDelayedExpansion 在顶部,然后使用 !errorlevel! 代替.延迟扩展发生在执行时,因此您可以看到括号内值的更改.

The solution is simple - delayed expansion. You need SETLOCAL EnableDelayedExpansion at the top, and then use !errorlevel! instead. Delayed expansion occurs at execution time, so then you are able to see the changes to the value within the parentheses.

SET 的帮助 (SET/?) 描述了关于 FOR 语句的问题和解决方案,但概念是相同的.

The help for SET (SET /?) describes the problem and the solution with regards to a FOR statement, but the concept is the same.

您还有其他选择.

您可以将代码从 IF 的正文中移动到没有括号的代码段,并使用 GOTOCALL 来访问代码.然后你可以使用 %errorlevel%.我不喜欢这个选项,因为 CALLGOTO 比较慢,而且代码不太优雅.

You can move the code from within the body of the IF to labeled sections of code without parentheses, and use GOTO or CALL to access the code. Then you can use %errorlevel%. I don't like this option because CALL and GOTO are relatively slow, and the code is less elegant.

另一种选择是使用 IF ERRORLEVEL N 而不是 IF !ERRORLEVEL!==N.(参见 IF/?)因为 IF ERRORLEVEL N 测试 errorlevel 是否 >= N,所以您需要按降序执行测试.

Another option is to use IF ERRORLEVEL N instead of IF !ERRORLEVEL!==N. (See IF /?) Because IF ERRORLEVEL N tests if errorlevel is >= N, you need to perform your tests in descending order.

REM Windows XP
ver | findstr /i "5.1." > nul
if '%errorlevel%'=='0' (
  choice /t:Y,5 "Do you want to automatically shutdown the computer afterwards "
  if ERRORLEVEL 2 set Shutdown=F
  if ERRORLEVEL 1 set Shutdown=T
)

这篇关于批处理文件“选择"命令的错误级别返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆