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

时间:2017-01-06 02:57:47

标签: windows batch-file

我正在处理基于文本的游戏父级的子批处理文件,我正在修改。根据我教授的指示,我必须使用if not statement。如果用户选择除了" Y"之外的任何东西,我想把它带到哪里。选择后,程序退出。我尝试使用'如果不退出'并且'如果不是y == y退出',但程序只是循环回到父批处理文件,无论选择后的输入。

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

:: 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

1 个答案:

答案 0 :(得分:0)

if y==y call scarymovie.bat
if not goto exit 

这不是它的工作原理......你对if not的意思是else 常用语法是:

if %variable%==value (
echo true
) else (
echo false
)

not仅用于否定条件,例如"变量是否为此值":

if not %variable%==value (
echo not value
)

此外,在您的脚本中还有无法访问的代码:

if y==y call scarymovie.bat

将始终评估为true,因为评论中提到了@JeffBlock。检查choice命令结果的正确方法是检查errorlevel的值:

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

如果不清楚,请随意提问:)