我正在MS-DOS 6.22中创建一个批处理菜单,我需要将if errorlevel
与goto
和call
混合使用。我的订单有问题,因为它们未按预期执行。除了call
之外,我无法使用file6.bat
来启动其他批处理文件,因为我需要保留环境变量。
这是菜单的示例:
:MENU
@echo off
echo [1] Choice 1
echo [2] Choice 2
echo [3] Choice 3
echo [4] Choice 4
echo [5] Choice 5
echo [6] Choice 6
echo [7] Choice 7
echo [8] Choice 8
echo [Y] Choice Y
echo [Z] Choice Z
choice /c12345678YZ /n /m "Select an option"
if errorlevel 10 call file7.bat
if errorlevel 9 file6.bat
if errorlevel 8 call file5.bat
if errorlevel 7 call file4.bat
if errorlevel 6 call file3.bat
if errorlevel 5 call file2.bat
if errorlevel 4 goto menu3
if errorlevel 3 goto menu2
if errorlevel 2 goto menu1
if errorlevel 1 call file1.bat
这就是行为:
goto menu3
]。请您帮助我了解如何解决此问题?
三个事实很重要:
[COMMAND.COM
的MS-DOS 6.22从批处理文件的顶部到底部处理另一行,除了命令GOTO用于继续批处理文件不是在下一行,而是在下一行在带有标签的行下方,该标签指定为命令GOTO的参数。] >>
DOS命令处理器continues
条件if errorlevel X
表示如果错误级别更大或等于1
if errorlevel X if not errorlevel X+1 command
。就像如果错误级别等于X,因为错误级别必须为大于等于X且等于X小于X + 1。这两个条件仅是如果错误等级等于X。示例:if errorlevel 6 if not errorlevel 7 call file3.bat
仅导致调用file3.bat
如果错误级别等于6。让我们一起看看由用户按下的键定义的用例。
用例1:
用户按下键Z。CHOICE.COM
退出,并为10
分配了值ERRORLEVEL
。if errorlevel 10
为truefile7.bat
由命令处理器调用。在完成对COMMAND.COM
的处理之后,file7.bat
继续在下一个IFif errorlevel 9
评估不是CHOICE.COM
的退出代码,但由批处理文件file7.bat
返回到主批处理文件的退出代码。 file7.bat
返回的退出代码很可能是0
,因此,此第二个条件以及下面的所有其他条件都将评估为false。用例2:
CHOICE.COM
退出,并为9
分配了值ERRORLEVEL
。if errorlevel 10
为falseif errorlevel 9
为truefile6.bat
。此处不使用命令CALL。因此,在完成COMMAND.COM
的处理后file6.bat
不会返回到主批处理文件。因此,if errorlevel 9 file6.bat
以下的所有内容在true的条件下都没有关系,因为批处理文件的处理以file6.bat
的完成处理结束。用例3:用户按下键8
CHOICE.COM
退出,并为8
分配了值ERRORLEVEL
。if errorlevel 10
为false。if errorlevel 9
为false。if errorlevel 8
为true,因此批处理文件file5.bat
由命令处理器调用COMMAND.COM
在完成对file5.bat
的处理之后,在下一个IF条件行上继续,除了它包含命令EXITif errorlevel 7
评估批处理文件file5.bat
返回到主批处理文件的退出代码,这很可能是0
,因此此第四条件和下面的所有其他评估为false。] >在用户按下键7,6或5
用例4:用户按下键4。
CHOICE.COM
退出,并为4
分配了值ERRORLEVEL
。if errorlevel 4
为truemenu3
的行下方的行上继续批处理文件处理。因此,其他IF在用户按下键[[3或2时,行为或多或少都相同。
用例5:
用户按下键1。CHOICE.COM
退出,并为1
分配了值ERRORLEVEL
。if errorlevel 9
为truefile1.bat
由命令处理器COMMAND.COM
在完成file1.bat
的处理后继续在线,除了在file1.bat
的处理中执行命令EXIT。ERRORLEVEL
的被叫批处理文件那样修改set variable=value
的值,则IF下的下一个menu3
的代码进行批处理。在最后一个条件if errorlevel 1
下的代码确定在所有那些批处理文件都以值0
退出的情况下,从该主批处理文件中<其他批处理文件接下来将发生什么。[第三个问题可以通过在最后一个条件if errorlevel 1
之后写在主批处理文件中来轻松解决,要么goto MENU
继续在顶部继续打印批处理文件并再次打印菜单,或者是goto ENDBAT
并在主批处理文件中最后一行:ENDBAT
继续在批处理文件的末尾继续处理批处理文件,这意味着完成该批处理文件的处理。但是对于潜在的问题1和2,必须确保主批处理文件的处理不在调用其他批处理文件之后在下一个命令行上继续进行,而是例如通过菜单进行。@echo off
:MainMenu
cls
echo [1] Choice 1
echo [2] Choice 2
echo [3] Choice 3
echo [4] Choice 4
echo [5] Choice 5
echo [6] Choice 6
echo [7] Choice 7
echo [8] Choice 8
echo [Y] Choice Y
echo [Z] Choice Z
choice /C:12345678YZ /N /M "Select an option:"
if errorlevel 10 set BatFile=7
if errorlevel 9 if not errorlevel 10 file6.bat
if errorlevel 8 if not errorlevel 9 set BatFile=5
if errorlevel 7 if not errorlevel 8 set BatFile=4
if errorlevel 6 if not errorlevel 7 set BatFile=3
if errorlevel 5 if not errorlevel 6 set BatFile=2
if errorlevel 4 if not errorlevel 5 goto Menu3
if errorlevel 3 if not errorlevel 4 goto Menu2
if errorlevel 2 if not errorlevel 3 goto Menu1
if errorlevel 1 if not errorlevel 2 set BatFile=1
call file%BatFile%.bat
set BatFile=
goto MainMenu
:Menu3
echo Here would be shown menu 3.
goto ENDBAT
:Menu2
echo Here would be shown menu 2.
goto ENDBAT
:Menu1
echo Here would be shown menu 1.
goto ENDBAT
:ENDBAT
BatFile
),并确保根据errorlevel
设置的条件,始终只有一个IF
条件为true CHOICE.COM
。
上面的代码导致用户按下键[[Y]]时执行,5]键到file6.bat
,而忽略了该主批处理文件中的其余行。在子菜单1、2或3的适当代码上按<< 2>,3
或4键继续进行批处理文件处理,但是对于1
8,并且7是相应的批处理文件,然后定义的环境变量BatFile
未定义,并且批处理文件的处理继续并再次打印主菜单,并让用户使用还有一个选择。
BatFile
,而不只是分配给它的编号。要调用的文件。在这种情况下,带有CALL的命令行为call %BatFile%.bat
。