Batch file 我能';无法理解批次的选择错误代码

Batch file 我能';无法理解批次的选择错误代码,batch-file,if-statement,cmd,choice,Batch File,If Statement,Cmd,Choice,如果我有这样的代码: @echo off choice /C BL /M "Clear screen or list actual directory" if errorlevel 2 goto l if errorlevel 1 goto b :l tree /f goto final :b cls goto final :final if errorlevel 1 goto l if errorlevel 2 goto b 我知道这确实有效,但我对errorlevel部分的一点感

如果我有这样的代码:

@echo off
choice /C BL /M "Clear screen or list actual directory"

if errorlevel 2 goto l
if errorlevel 1 goto b

:l
tree /f
goto final

:b
cls
goto final

:final
if errorlevel 1 goto l
if errorlevel 2 goto b
我知道这确实有效,但我对errorlevel部分的一点感到困惑。 我先写了相同的代码,但如下所示:

@echo off
choice /C BL /M "Clear screen or list actual directory"

if errorlevel 2 goto l
if errorlevel 1 goto b

:l
tree /f
goto final

:b
cls
goto final

:final
if errorlevel 1 goto l
if errorlevel 2 goto b
这样它就不能正常工作了,它只会记住错误代码1。如果按第二个选项,则该选项不起作用。
我真的很想知道为什么错误的顺序很重要,如果一个批应该逐行执行,还是我错了?
简而言之,我想了解的是错误代码在这里是如何工作的

C:\>if/?
C:\>if /?
...
IF [NOT] ERRORLEVEL number command
...
ERRORLEVEL number   Specifies a true condition if the last program run returned
                    an exit code equal to or greater than the number specified.
... 如果[NOT]错误级别编号命令 ... ERRORLEVEL number指定上一次程序运行返回时的真实条件 等于或大于指定数字的退出代码。
换句话说,
if errorlevel 1执行任何errorlevel(0=无错误除外),因为它们都等于或大于1。

C:\>if/?
...
如果[NOT]错误级别编号命令
...
ERRORLEVEL number指定上一次程序运行返回时的真实条件
等于或大于指定数字的退出代码。

换句话说,
如果errorlevel 1执行任何errorlevel(0=无错误除外),因为它们都等于或大于1。

只是一个提示,当使用连接到goto标签的
%errorlevel%
变量时,事情会很简单:

@echo off
choice /C BL /M "Clear screen or list actual directory"
goto :choice%errorlevel%

:choice1 B
tree /f
goto final

:choice2 L
cls
goto final

:final
pause

提示一下,当使用附加到goto标签的
%errorlevel%
变量时,事情会变得非常简单:

@echo off
choice /C BL /M "Clear screen or list actual directory"
goto :choice%errorlevel%

:choice1 B
tree /f
goto final

:choice2 L
cls
goto final

:final
pause

带有
if errorlevel 1
的经典表单实际上被评估为
if errorlevel为1或更大
,因此第一个数字越小,第二个if就不会执行。
if errorlevel 1 if not errorlevel 2
只会在1上触发。从选项帮助文件:注意:errorlevel环境变量设置为从选项集中选择的键。列出的第一个选项返回值1,第二个选项返回值2,依此类推。如果用户按下的键无效,该工具将发出警告蜂鸣音。如果工具检测到错误条件,则返回的ERRORLEVEL值为255。在批处理程序中使用ERRORLEVEL参数时,按降序列出它们。如果errorlevel 1
的经典形式实际上被评估为
如果errorlevel为1或更大
,则使用较小的数字首先执行第二个if,根本不会执行。
如果errorlevel 1如果不是errorlevel 2
将仅在1上触发。从选择帮助文件:注意:errorlevel环境变量设置为从选项集中选择的键的索引。列出的第一个选项返回值1,第二个选项返回值2,依此类推。如果用户按下的键无效,该工具将发出警告蜂鸣音。如果工具检测到错误条件,则返回的ERRORLEVEL值为255。在批处理程序中使用ERRORLEVEL参数时,请按降序列出它们。