为什么我的选择计划不起作用?

时间:2014-06-12 21:30:15

标签: batch-file

我正在制作Windows Batch程序,并且具有以下内容:

choice /c "12" /n /t 10 /d 1 /m "> "
if %errorlevel%=="255" goto start
if %errorlevel%=="2" goto choice2
if %errorlevel%=="1" goto choice1
if %errorlevel%=="0" goto start

:choice1
echo choice1
goto eof

:choice2
echo choice2
goto eof

:eof

当我运行它时,我通过在键盘上按“2”来选择选项2,而%ERRORLEVEL%确实说我选择了“2”。但是,当谈到IF语句时,似乎总是说“choice1”。我做错了什么?

2 个答案:

答案 0 :(得分:0)

如果您要检查它是否等于某个值,则应删除""

答案 1 :(得分:0)

两件事:

  1. errorlevel可以重置。当您有一长串代码检查返回值时,最好将%errorlevel%分配给其他变量并检查它。

  2. 正如@NielsPeeren所提到的,你应该与引用保持一致 - 要么引用表达的两边,要么不引用。

  3. 试试这个:

    choice /c "12" /n /t 10 /d 1 /m "> "
    set response=%errorlevel%
    if "%response%"=="255" goto start
    if "%response%"=="2" goto choice2
    if "%response%"=="1" goto choice1
    if "%response%"=="0" goto start