Solved Nested IF in batch.. confusing?????

October 23, 2014 at 18:17:07
Specs: Windows 7
alright, i am writing a program to do many different things, and i need the nested if statement to work, but i have tried it many different ways and it doesnt work.... i need some feed back fast, please because i am stuck.... here is my code:

@echo off
echo Did you have school today?
echo.
set/p ans=
echo.
if %ans% ==yes(
echo Oh, how was it? good or bad?
echo.
set/p B=
echo.
if %B% ==good(
echo that is good...
echo.
echo what is your favorite subject? math, science, history, english?
echo.
set /p C=
echo.
if %C% ==math (echo Oh, cool.)
if %C% ==science (echo That is cool.)
if %C% ==history (echo Nice!)
if %C% ==english (echo That is great!)
)
if %B% ==bad (echo Oh, I am sorry...)
)
if %ans% ==no (echo Oh, okay, well... nevermind.)
pause

i need it here so that it will run, because it doesnt....

then here it doesnt work for some reason either:

echo What can I help you with?
echo.
set/p help=
echo.
if %help% ==nothing (echo Oh ok, tell me when you do...)
if %help% =="nothing but how are you?" (echo Good, as always, thanks for asking.
echo.
set/p D=
echo.
if %D% =="your welcome" (echo ...)
if %D% =="no problem" (echo ...)
if %D% =="don't mention" it (echo ...)
if %D% =="sure thing" (echo ...))
if %help% =="what's your favorite color" (echo My favorite color is black.)
if %help% =="what's your favorite color?" (echo My favorite color is black.)
if %help% =="what is your favorite color" (echo My favorite color is black.)
if %help% =="what is your favorite color?" (echo My favorite color is black.)


it just wont work, please give a very detailed response and lots of examples please, i am very very slow at learning new things................


See More: Nested IF in batch.. confusing?????

Report •


#1
October 23, 2014 at 22:01:07
✔ Best Answer
Inside parenthesis sets (called a "code block"), variables with percents may as well be considered "valueless". You have to use "enabledelayedexpansion" and substitute ! for % on all variables. Here's your code with that in mind:

@echo off
:: YOU MUST ADD THIS NEXT LINE UNLESS SYSTEM DEFAULT IS ACTIVE
setlocal enabledelayedexpansion
echo Did you have school today?
echo.
set/p ans=
echo.
if %ans% ==yes (
pause
echo Oh, how was it? good or bad?
echo.
set/p B=
echo.
if !B! ==good (
echo that is good...
echo.
echo what is your favorite subject? math, science, history, english?
echo.
set /p C=
echo.
if !C! ==math (echo Oh, cool.)
if !C! ==science (echo That is cool.)
if !C! ==history (echo Nice!)
if !C! ==english (echo That is great!)
)
if !B! ==bad (echo Oh, I am sorry...)
)
if %ans% ==no (echo Oh, okay, well... nevermind.)
pause
::-------
For a good tutorial, just type:
FOR /?
IF /?
Also, when using SET /P, always set your variables to null, as a practice.
set x=
set /p x=what?

As for "part two", here's the correct version that uses dbl quotes to fix:

echo What can I help you with?
echo.
set/p help=
echo.
if "%help%" =="nothing" (echo Oh ok, tell me when you do...)
if "%help%" =="nothing but how are you?" (echo Good, as always, thanks for asking.
echo.
set/p D=
echo.
if %D% =="your welcome" (echo ...)
if %D% =="no problem" (echo ...)
if %D% =="don't mention" it (echo ...)
if %D% =="sure thing" (echo ...))
if %help% =="what's your favorite color" (echo My favorite color is black.)
if %help% =="what's your favorite color?" (echo My favorite color is black.)
if %help% =="what is your favorite color" (echo My favorite color is black.)
if %help% =="what is your favorite color?" (echo My favorite color is black.)

::-----------
Your version, lacking dbl quotes, looks like this, and will never be TRUE:

if %help% ==nothing (echo Oh ok, tell me when you do...)
looks like:
if "%help%"=="nothing (echo OH ok, tell me when you do...("
(note the dbl quotes: the comparator includes all the parentheses etc!)
even if the user types the exact string:
nothing (echo Oh ok, tell me when you do...)

to get a TRUE condition, the next statement cannot also be true:

if "%help%" =="nothing but how are you?" (echo Good, as always, thanks for asking.

because "%help%" is already "nothing (echo Oh ok, tell me when you do...)"

even with the quotes properly placed, you're comparing two different strings that will never be equal.

Ya need to get in habit of ALWAYS using dbl quotes on your IF statements, to avoid this disaster. If the item might contain a dbl quote, then more extreme measures are called for, but we'll not go there yet.


Report •
Related Solutions


Ask Question