1

有人可以帮我处理我的批次吗?我试图在我的批次开始时逃脱,但它不起作用。我敢肯定这很简单,但我不是这种编程的普通用户。

cls
@echo off
set usr_out=y
set /p usr_out=Press [N] to cancel:

if NOT %usr_out% == Y goto myend
if NOT %usr_out% == y goto myend

echo in

pause
exit

:myend
echo out
pause

编辑:对不起,我忘了详细说明上面的代码只有echo out在我不输入值时才会进入该行。

4

3 回答 3

5

您将默认值设置%usr_out%小写 y

set usr_out=y

但然后检查变量是否等于大写 Y

if NOT %usr_out% == Y goto myend

由于y并且Y确实不相等,因此您的脚本会正确跳转到:myend此时。

你需要在这里修正你的逻辑。如果您想在不满足所有给定数量的条件时跳过代码块,则必须批量使用类似的东西:

if "%usr_out%"=="y" goto continue
if "%usr_out%"=="Y" goto continue
goto myend
:continue

但是,在您的特定情况下,您可以使用更简单的方法,因为您只需要对一个字母进行不区分大小写的检查:

if /i not "%usr_out%"=="y" goto myend

在不同的说明:而不是平原exit,我建议使用goto :eof(跳转到脚本的末尾)或exit /b(退出批处理脚本而不终止cmd.exe)。否则,在命令提示符下手动运行脚本可能会无意中终止命令提示符窗口。

于 2013-01-20T23:24:12.760 回答
0

简单的问题是您在“==”和其他命令或单词之间使用空格,例如:

if NOT %usr_out% == Y goto myend

但你必须使用这个:

if NOT %usr_out%==Y goto myend
于 2014-08-25T12:53:23.620 回答
0

To the second half of your question:

when I don't enter a value

You can add this check:

set /p usr_out=Press [N] to cancel:
if "x"=="x%usr_out%" set usr_out=y

This checks whether the variable has no value, or the empty string as a value.

于 2018-06-08T17:22:04.407 回答