English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How do I perform an IF ELSE loop in BATCH?

For example.....
IF %variable%=="Hello" ECHO Hi
ELSE ECHO Sorry, I dont understand you!

Is this the correct way to do it. If it is not, please give me the correct way of performing a IF ELSE loop in BATCH!

Thanks!

2007-04-11 22:46:37 · 4 answers · asked by Cal R 2 in Computers & Internet Programming & Design

No, I have already defined %variable%! Please be more clear in your answer!

2007-04-11 23:11:22 · update #1

No, I have already defined %variable%! Please be more clear in your answer!

2007-04-11 23:11:31 · update #2

There is a ELSE loop because I saw it on a tutorial one but I cant find it anymore!

2007-04-11 23:12:43 · update #3

4 answers

You definitely can use IF-ELSE in batch commands. You're on the right track, just a little tweaking needed here.

First, let's handle a couple other command processing items, then we'll get to the ELSE statement.

1) When referencing variables that have the possibility of being blank, it is good practice to enclose the check in quotes to prevent trying to check a null value - I'll demonstrate that first.

For example, if you had a batch file that accepted a commandline switch (we'll call it %switch%) and you wanted to check to see if the person entered one, or see if they entered one correctly, then you would have a line like

if "%switch%" == "" goto :SYNTAX

In this case, if no switches were entered on the commandline that called the batch, then the check would be interpreted as,

if "" == ""

which is true and would kick the person to the syntax part of your batch file. If you didn't have the quotes around the variable during the check, then the line would be parsed as,

if ==""

which is attempting to compare a null value to "" and would fail.

2) When attempting to execute additional commands whether using IF-ELSE or using the FOR-DO, it is good practice to enclose the separate commands in parentheses (..). In your case, it is also required. This allows DOS to figure out that there are two separate ECHO statements.

Otherwise, DOS blindly sees the first "echo" statement and will echo the rest of the line. For instance, if you had the following all on one line,

IF "%variable%"=="Hello" ECHO Hi ELSE ECHO Sorry, I dont understand you!

... and your variable equals "Hello", then it would be processed as,

if "Hello" == "Hello" (this is true, so do the next thing)
ECHO Hi ELSE ECHO Sorry, I dont understand you!

so, on the screen you would see
ECHO Hi ELSE ECHO Sorry, I dont understand you!

which isn't what you want, but once DOS sees an "ECHO" command, it will try to echo everything behind it including the word "else" which it now sees as plain text instead of as a command.

How do we fix that? With parentheses. Parentheses are a way to tell DOS that there are separate commands that it should handle invidividually. When used with IF-ELSE, then DOS knows how to parse the ELSE. This isn't always required, but it is for your example because your IF includes an ECHO, so DOS needs to know where to look for the ELSE.

Here's how your command should look ... again, all on one line ... Answers can wrap stuff to two lines ...


IF "%variable%"=="Hello" (ECHO Hi) ELSE (ECHO Sorry, I dont understand you!)

============
Note: Capitalization counts. "HELLO" does not equal "Hello".

===========================
===========================
I checked out your other question and the answer you chose. I have a couple of notes for that and modifications to the example batch file.

1) On the line 'set userinput=%variable%', don't use quotes around %variable% ; otherwise, the actual text of the userinput variable will contain quotes and this could cause issues with other kinds of checks.

2) You also need to check to see if the user just hits enter without entering any text. If you don't do this you could have errors. For example, if the person has already entered 'how are you' and has seen 'Fine', then they just hit Enter on the next 'You Say', then userinput still equals 'how are you' and the user sees 'Fine' again.

So, use that null check like

if "%userinput%"=="" goto :START

3) If you're going to use the IF-ELSE to have the computer respond "I don't understand you", then you'll have to have a way for each IF statement to get back to :START. If you don't, then you're going to see a LOT of "I don't understand you"s for each input by the user.

If they enter "how are you", then the batch says "fine"

Then the batch goes to the next line
if "%userinput%"=="no" (echo I see) else (echo Sorry, I don't understand you)

and since the check is not true "how are you" is not "no", then the batch will respond "Sorry, I don't understand you"

On the screen it would look like,

You say:how are you
Fine
Sorry, I don't understand you

You Say:

.. so that's not really what you want. So you have to include a way for each line to get back to :START if the user puts something good or bad on the line. That's accomplished with the "&" function which groups commands together ... do this & that.

========================

And a final note, since capitilization counts, you might want to include a subroutine that will switch the user's input to lower or upper case. That way, if they put in "hello" or "Hello" or "HELLO" or even "hEllO" it will all be parsed as the same word.

Here's how that example batch would work including a subroutine that converts the userinput to all CAPS so that they don't have to know what case to enter and you only need to check against one case.

===============================
@ECHO OFF

:START


set /p variable=You Say:

set userinput=%variable%

if "%userinput%"=="" goto :START

CALL :UPPER

if "%userinput%"=="HOW ARE YOU" (echo Fine & goto :START) else (echo I don't understand you & goto :START)
if "%userinput%"=="NO" (echo I see. & goto :START) else (echo I don't understand you & goto :START)

:: chance to get out
echo. & pause

:: good practice to make sure your batch doesn't
:: fall down into your sub-routines using a goto EOF
GOTO :EOF

::::::::::::::::::::::::::::
:: SUB-ROUTINES
::::::::::::::::::::::::::::
: UPPER

:UPPER
set userinput=%userinput:a=A%
set userinput=%userinput:b=B%
set userinput=%userinput:c=C%
set userinput=%userinput:d=D%
set userinput=%userinput:e=E%
set userinput=%userinput:f=F%
set userinput=%userinput:g=G%
set userinput=%userinput:h=H%
set userinput=%userinput:i=I%
set userinput=%userinput:j=J%
set userinput=%userinput:k=K%
set userinput=%userinput:l=L%
set userinput=%userinput:m=M%
set userinput=%userinput:n=N%
set userinput=%userinput:o=O%
set userinput=%userinput:p=P%
set userinput=%userinput:q=Q%
set userinput=%userinput:r=R%
set userinput=%userinput:s=S%
set userinput=%userinput:t=T%
set userinput=%userinput:u=U%
set userinput=%userinput:v=V%
set userinput=%userinput:w=W%
set userinput=%userinput:x=X%
set userinput=%userinput:y=Y%
set userinput=%userinput:z=Z%

2007-04-13 01:31:49 · answer #1 · answered by Kevin 7 · 3 0

Seems like you need to need to establish, if %varible% = "Hello" first, if not ..is it an input varible Therefore. if I do not type in Hello,, then your ELSE statemnt will Echo your response statement. My thinking is that your IF statement will end and that it. I think you will need a nested IF Statment to loop, otherwise, your program will quit regardless of answer. Becareful not to get stuck in an ENDLESS LOOP. Good luck.

2007-04-11 22:57:34 · answer #2 · answered by Chepe 1 · 0 2

There is no ELSE command in batch programming

IF %variable%=="Hello" goto hi
ECHO Sorry, I dont understand you!
goto end

:hi
echo Hi

:end

2007-04-11 23:08:48 · answer #3 · answered by Anonymous · 0 2

cal R try this link n learning it for yourself

http://www.google.co.uk/search?hl=en&q=learning+dos+programing&meta=

2007-04-12 03:50:52 · answer #4 · answered by Joe_Young 6 · 0 2

fedest.com, questions and answers