Windows批处理使用If和If not命令

Windows批处理使用If和If not命令,windows,batch-file,Windows,Batch File,我正在为我正在修补的基于文本的游戏家长编写一个子批处理文件。我必须按照教授的指示使用if not语句。我想知道,如果用户在选择后选择了除“Y”之外的任何内容,程序将退出。我尝试使用'if not exit'和'if not y==y exit',但无论选择后的输入是什么,程序都会循环回到父批处理文件 我如何保证我的if not exit语句始终关闭程序?注意:程序是从桌面运行的 :: Child Batch File @echo off Title Halloween...... color 0

我正在为我正在修补的基于文本的游戏家长编写一个子批处理文件。我必须按照教授的指示使用if not语句。我想知道,如果用户在选择后选择了除“Y”之外的任何内容,程序将退出。我尝试使用'if not exit'和'if not y==y exit',但无论选择后的输入是什么,程序都会循环回到父批处理文件

我如何保证我的if not exit语句始终关闭程序?注意:程序是从桌面运行的

:: Child Batch File
@echo off
Title Halloween......
color 06  
echo set speech = wscript.Createobject("SAPI.spvoice") >> "temp.vbs"
set text=You cannot kill the boogie Man!
echo speech.speak "%text%" >> "temp.vbs"
start temp.vbs
pause
del temp.vbs
Echo Another Round??
Pause
cls
Choice
if y==y call scarymovie.bat
if not goto exit 
:exit
父批处理文件:

@Echo Off
Title Slim Shady - Scary Movie
color 04
:start
set /a counter+=1
Echo You Have Run This Program %counter% times
Echo %counter% >> answers.txt
Echo Greetings Traveler, Scrawl Your Name Across The Screen
set /p name=
Echo %name% >> answers.Txt
Echo What's Your Favorite Scary Movie??
Pause
cls
Echo Press 1 for Halloween
Echo Press 2 for The Exorcist
Echo Press 3 for I Doesn't Afraid No Ghost
set /p movie=
Echo %movie% >> answers.txt
if %movie%==1 call Halloween.bat
if %movie%==2 call Exorcist.bat
if %movie%==3 Start www.SesameStreet.org
Pause
Cls
这不是它的工作原理。。。如果不是
的意思是
else

常用语法是:

if %variable%==value (
echo true
) else (
echo false
)
not
仅用于否定条件,如“变量是否除not此值以外的任何内容”:

脚本中还有无法访问的代码:

if y==y call scarymovie.bat
将始终按照注释中的@JeffBlock计算为真。检查choice命令结果的正确方法是检查errorlevel的值:

choice
Goto label%errorlevel%
:label0
echo Y
Goto nextStep
:label1
echo N
Goto nextStep

如果有不清楚的地方,请随时提问:)

Y==Y的计算结果总是正确的。为什么在父脚本中使用set/p作为子脚本中的选项?用set/p再次替换choice=,然后IF NOT%再次%==y退出/b hanks,我最后用一个变量来做IF&IF NOT语句。啊,我明白了,对errorlevels没有任何线索。我最终选择了“如果”和“如果”两种方式,对于我想要完成的事情,我的头脑要简单得多。希望我们今天在课堂上讨论选择:)。谢谢你的帮助!欢迎你:如果我的答案是有帮助的,解决了你的问题,考虑通过点击旁边的灰色复选标记来接受它。这不仅有助于保持网站的清洁,还将奖励您:)
if y==y call scarymovie.bat
choice
Goto label%errorlevel%
:label0
echo Y
Goto nextStep
:label1
echo N
Goto nextStep