Windows批处理,如果选择了错误的值

问题描述 投票:0回答:1

我正在尝试在一个小的测试批处理文件中使用CHOICE命令。这是我的代码:

@ECHO Off
choice /M "Is this correct"
IF ERRORLEVEL 1 echo This is correct
IF ERRORLEVEL 2 echo This is not correct
echo %errorlevel%

[当我按下y时我得到了This is correct,但是当我按下n时我得到了This is correctThis is not correct

为什么第一个选项被触发?从echo %errorlevel%中可以看到errorlevel是2。

[这里我以echo为例,它是我有一个goto的实际batch文件,并且我一直都在触发第一个goto。

windows batch-file
1个回答
0
投票

尝试这样:

@@ ECHO关闭

choice / N / C:yn / M“这是正确的吗?”

如果%ERRORLEVEL%== 1 echo这是正确的

如果%ERRORLEVEL%EQU 2 echo这是不正确的

echo%errorlevel%

练习基本指令,例如ifchoice

© www.soinside.com 2019 - 2024. All rights reserved.