如果设置批处理错误

If Set Batch error

我正在尝试将 SET /p 用于一个简单的 yes/no 场景,但我的语法似乎很奇怪:

SET /p var="Knock Knock! "
IF NOT %VAR% == "Who is there" (ECHO "NOPE")
SET /p var="Orange! "
IF NOT %VAR% == "Orange who?" (ECHO "NOPE)
ECHO "Orange you glad I didn't say banana?"

很抱歉用一个简单的问题浪费你的时间

你应该引用包含空格的变量,例如with:

IF NOT "%VAR%"=="Who is there" ECHO "NOPE"

否则转化为:

IF NOT Who is there=="Who is there" ECHO "NOPE"

并抱怨 is


做出这样的改变,以及其他一些改进,会产生类似这样的结果:

    @echo off
:loop1
    set /p var=Knock Knock! 
    if not "%var%"=="Who is there?" (
        echo Please answer 'Who is there?'
        goto :loop1
    )
:loop2
    set /p var=Orange! 
    if not "%var%" == "Orange who?" (
         echo Please answer 'Orange who?'
         goto :loop2
    )
    echo Orange you glad I didn't say banana?

和运行因此:

C:\USERS\paxdiablo\Documents> kkjoke
Knock Knock! What?
Please answer 'Who is there?'
Knock Knock! NO!
Please answer 'Who is there?'
Knock Knock! Who is there?
Orange! Oh, is it 'orange of steak knives at half price'?
Please answer 'Orange who?'
Orange! What about 'orange, orange, my kingdom for orange'?
Please answer 'Orange who?'
Orange! Well, IS IT?
Please answer 'Orange who?'
Orange! Orange who?
Orange you glad I didn't say banana?