Batch file with multiple if statements

ironflare

Honorable
Dec 31, 2012
6
0
10,510
Hello,

I am trying to make a batch file which echos text based on an input.

Here is the code I have so far:

---------------------------------------------

@echo off
set /p input=

If %input%=="hello" or %input%=="hi" or %input%=="hey" or %input%=="yo" or %input%=="hello?" or %input%=="hi?" or %input%=="hey?" or %input%=="yo?"
@echo off
echo Hello. How are you today?

If %input%=="I am good" or %input%=="Good" or %input%=="Great" or %input%=="I am great" or %input%=="Awesome" or %input%=="I am awesome"
@echo off
echo I am glad to hear that.

If %input%=="Who are you" or %input%=="What are you" or %input%=="Who are you?" or %input%=="What are you?"
@echo off
echo I am a program.

Else
@echo off
echo I don't understand that.

-----------------------------------------------

when I save the file and run it, it all at once echos the first three results, then tells me that 'else' is not an operable command and then the program closes. What am I doing wrong?
 

chugot9218

Honorable
Use Pause in between your statements to do some debugging. I would guess you have a problem with your input variable or the conditions, also, you are not creating a true "if/else-if" statement, just several If's and an If-Else. I found this suggestion online that basically uses jump statements:

@echo off
echo.
set /p num1=Enter a number:
echo.
set /p num2=Enter another number:
echo.
if %num1%==%num2% goto true
if not %num1%==%num2% goto false
:true
echo The two numbers you entered were the same.
echo.
pause
exit
:false
echo The two numbers you entered were different.
echo.
pause
exit
 

chugot9218

Honorable
Your issue is you are not refreshing the input variable, you have to re-prompt for entry or use separate variables.

Also, I have not used If much in .bats, but your condition is returning true for every possibility, or it is not doing a check at all, because any input results in Hello. It does not know what to do in the case of a false and simply drops through to the next statement.
 

ironflare

Honorable
Dec 31, 2012
6
0
10,510


Thanks for investigating. I added pause in between my commands, and it just echoed each response in order after each key press, as would be expected.

So, would my best option be, then, to nest the if commands, each command contained in the else statement of the one before it? If so, how would I do that? I know about that method, just not how to program it.

Also, I'm certain that I don't have a problem with my input variable because (here's where it gets complicated :??: ) my program evaluates the input and then writes it to a VBScript file where an appropriate response to the user's input is found and then spoken using
set ObjVoice=CreateObject("SAPI.SpVoice")
ObjVoice.Speak "texttobespoken"
:sweat:

This part works, and a response is spoken, just not echoed on screen using the batch file.
As far as programming the batch file, this section, apart from proving that the input command is intact, can be completely ignored inasmuch as it is essentially just running a different file.

The batch file then loops back to the beginning to receive another input (there's the re-prompting for entry), so as to create the illusion of a conversation.

Again, thanks for your help. Any more thoughts? ideas? I really appreciate it.
 

chugot9218

Honorable
Well if I understood what your trying to do a little better I could help more, but I don't quite get how with the batch file you presented you are writing anywhere. You are saying it takes input once, writes it to a VBscript (which I don't see in the batch presented), and then responds to the initial input, and it queries for input again before each if?

Also, I don't believe the command line supports "or" inside of an If statement, I could not get any of your If's to correctly identify my input. If I change the IF to match only 1 statement (also text does not have to be in quotes that could be messing you up too) then my input matches an If check:

@echo off
set /p input=

If %input%==hello goto :hello

pause

If %input%=="I am good" or %input%=="Good" or %input%=="Great" or %input%=="I am great" or %input%=="Awesome" or %input%=="I am awesome"
@echo off
echo I am glad to hear that.

If %input%=="Who are you" or %input%=="What are you" or %input%=="Who are you?" or %input%=="What are you?"
@echo off
echo I am a program.

Else
@echo off
echo I don't understand that.

:hello
echo Hello. How are you today?


Using a .bat for this makes it somewhat unwieldy, you'd be better off using another programming language I think. This is how you would check multiple If/or's and is very ugly:

If %input%==I am good goto :response2
If %input%==Good goto :response2
If %input%==Great goto :response2
If %input%==I am great goto :response2
If %input%==Awesome goto :response2
If %input%==I am awesome goto :response2


:response2
@echo off
echo I am glad to hear that.


If it does not match the first input, it will simply fall through and check the next value, but all lead to :response2 should they find a match.


 

ironflare

Honorable
Dec 31, 2012
6
0
10,510


Thanks again for brainstorming. Here is the entire code, with the commands to write to VBScript. Just by the way, you're probably going to look at the five or so lines of VBScript with all of the "hello"s and "hi"s and "hey"s and "yo"s and go :eek: . But that's to cover punctuation, so in case someone puts an exclamation point or a period or a question mark at the end, it won't give a syntax error. Also, I added i think two more commands(One to agree with you, one to launch a web browser), but that shouldn't matter where the batch file is concerned.

setlocal enableextensions enabledelayedexpansion
@echo off
:begin
cls
@echo off
set /p input=Query:
@echo off
echo StrText="%input%" > vocalize.vbs
echo if StrText="hello" or StrText="hi" or StrText="hey" or StrText="yo" or StrText="hello?" or StrText="hi?" or StrText="hey?" or StrText="yo?" or StrText="hello!" or StrText="hi!" or StrText="hey!" or StrText="yo!" or StrText="Hello" or StrText="Hi" or StrText="Hey" or StrText="Yo" or StrText="Hello?" or StrText="Hi?" or StrText="Hey?" or StrText="Yo?" or StrText="Hello!" or StrText="Hi!" or StrText="Hey!" or StrText="Yo!" or StrText="hello." or StrText="hi." or StrText="hey." or StrText="yo." or StrText="Hello." or StrText="Hi." or StrText="Hey." or StrText="Yo." then >> vocalize.vbs
echo set ObjVoice=CreateObject("SAPI.SpVoice") >> vocalize.vbs
echo ObjVoice.Speak "hello. how are you today?" >> vocalize.vbs
echo Elseif StrText="I am good" or StrText="good" or StrText="great" or StrText="I am great" or StrText="awesome" or StrText="I am awesome" or StrText="I am good." or StrText="good." or StrText="great." or StrText="I am great." or StrText="awesome." or StrText="I am awesome." or StrText="i am good" or StrText="Good." or StrText="Great." or StrText="i am great" or StrText="Awesome." or StrText="i am awesome" or StrText="Great" or StrText="Good" or StrText="Awesome" then >> vocalize.vbs
echo set ObjVoice=CreateObject("SAPI.SpVoice") >> vocalize.vbs
echo ObjVoice.Speak "I am glad to hear that." >> vocalize.vbs
echo Elseif StrText="what are you?" or StrText="who are you?" or StrText="what are you" or StrText="who are you" or StrText="What are you" or StrText="Who are you" or StrText="What are you?" or StrText="Who are you?" then >> vocalize.vbs
echo set ObjVoice=CreateObject("SAPI.SpVoice") >> vocalize.vbs
echo ObjVoice.Speak "I am a program." >> vocalize.vbs
echo Elseif StrText="where are you?" or StrText="where are you" or StrText="Where are you?" or StrText="Where are you" then >> vocalize.vbs
echo set ObjVoice=CreateObject("SAPI.SpVoice") >> vocalize.vbs
echo ObjVoice.Speak "I reside inside your computer." >> vocalize.vbs
echo Elseif StrText="That's great." or StrText="that's great." or StrText="That's great!" or StrText="that's great!" then >> vocalize.vbs
echo set ObjVoice=CreateObject("SAPI.SpVoice") >> vocalize.vbs
echo ObjVoice.Speak "Yes, it is." >> vocalize.vbs
echo Elseif StrText="Internet" or StrText="Opera" or StrText="Website" or StrText="internet" or StrText="opera" or StrText="website" then >> vocalize.vbs
echo set ObjVoice=CreateObject("SAPI.SpVoice") >> vocalize.vbs
echo ObjVoice.Speak "OK, I'll launch your web browser." >> vocalize.vbs
echo Dim objShell >> vocalize.vbs
echo Set objShell = WScript.CreateObject( "WScript.Shell" ) >> vocalize.vbs
echo objShell.Run("opera.exe") >> vocalize.vbs
echo Set objShell = Nothing >> vocalize.vbs
echo Else >> vocalize.vbs
echo set ObjVoice=CreateObject("SAPI.SpVoice") >> vocalize.vbs
echo ObjVoice.Speak "I don't understand that." >> vocalize.vbs
echo End If >> vocalize.vbs
start vocalize.vbs

If %input%==I am good goto :response2
If %input%==Good goto :response2
If %input%==Great goto :response2
If %input%==I am great goto :response2
If %input%==Awesome goto :response2
If %input%==I am awesome goto :response2



:response2
@echo off
echo I am glad to hear that.

pause
cls

goto begin
end local

I inserted your code at the bottom, but when I saved it and ran it, typed something and hit enter, the program closed instantly; It echoed something about 'unexpected', it closed before i could read it.

Then, I tried putting a pause before cls towards the bottom of the code.
I saved it, ran it, and typed "Hello". Then it responded by echoing "I am glad to hear that." Wait, what?! That wasn't one of the IF statements. What it makes me think is that it's ignoring the IFs at the beginning of the lines.

I was just wondering, is there an "End If" type of thing for batch like there is for VBScript, because I have a suspicion that that might be the solution to the problem.

Thanks!