If statements not working in batch file - batch-file

I made this code for something to remind me about certain things I need to do, but for some reason the code isn't working.
#echo off
cls
echo Welcome to ChoreHub
echo -------------------------------------------
echo Today's List:
echo * Do School
echo * Practice Piano
echo * Check to Make Sure House is Clean
echo * Check to Make Sure Room is Clean
echo -------------------------------------------
echo What chore have you finished? (1, 2, 3, 4)
set /P chore=
if %chore%=="1" goto :1
if %chore%=="2" goto :2
if %chore%=="3" goto :3
if %chore%=="4" goto :4
goto bro
When I put in a right answer, it skips over the if statements and goes straight to the bottom.
I'm not sure why, I am a little rusty on batch files though. If anyone could help me that would be great, thanks!

you haven't set anything? where is 1,2,3 and bro? where is it supposed to go??
you need to add this
:1
echo 1 is selected
goto :end
2:
echo 2 is selected
goto :end
:3
echo 3 is selected
goto :end
:end
check this fully working batch example

Hard to tell without seeing all your code, but I think, your problem is here:
if %chore%=="1" goto :1
You are comparing your variable with "1" (not just 1 what you seem to think)
You need to enter "1" ( Quote 1 Quote ) to match this.
Better code:
if "%chore%"=="1" goto :1
(Note: if %chore%==1 seems to be correct too, but will give a syntax error, if %chore% is empty)

Related

Skipping to next step in Batch program

#echo off
setlocal EnableDelayedExpansion
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is !name2!^^! I'm an AI. I'm here to help with your lazy
Butt :D^^!
timeout /t 3 >null
echo!name2!: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >null
echo A. My name is
echo B. Express
set /p input=
if !input! equ B goto Writing2
cls
echo!name2!: Hello !name!, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
set /p input=!name!:
if !input! equ 1 goto Writing
if !input! equ Yes goto Writing
if !input! equ 2 exit
if !input! equ No exit
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route----->
goto Writing
Hello!
What I am trying to do here is skipping all the steps and go straight to "Writting2" if typed Express.
I am almost done with this fun program but I can't figure out a good way skip all the steps.
When I type "Express or select 'B'
it crashes.
but If I write a name the program works as usual!
Thanks in Advance!
Sorry in Advance if I did something wrong in the community.
This is a way you can do what you want:
#echo off
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is %name2% I'm an AI. I'm here to help with your lazy
::echo Butt :D^^!
timeout /t 3 >nul
echo %name2%: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >nul
echo A. My name is
echo B. Express
choice /c:AB>NUL
if errorlevel 2 goto Writing2
set /p "name=Enter your name: "
:Writing
cls
echo %name2%: Hello %name%, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
choice /c:1Y2N>NUL
if errorlevel 4 goto exit
if errorlevel 3 goto exit
if errorlevel 2 goto Writing
if errorlevel 1 goto Writing
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route-----^>
goto Writing
:exit
exit /b
Remember: Variables can be accessed by %variable_name% and you can set them by set "variable_name=variable_value" as #Compo mentioned above.
It is better to use choice /c option in your future batchfiles. A disadvantage of this option is that you cannot enter a string with more than 1 character, but it does it's own errorhandling, so you don't have to deal with invalid responses. Also, when you write
echo Anyway. Initiating EXPRESS Route----->
> symbol causes problems as it is a redirection character and should be escaped:
echo Anyway. Initiating EXPRESS Route-----^>

ls it possible to have 2 words to compare with IF?

#echo off
:menu
echo.
echo Hello! Would you like to play this Quiz game?
ping localhost -n 2 >nul
goto Q1
:Q1
echo.
echo Who is the CEO of Google?
set /p Q1A=
if %Q1A% equ Sundar Pichai goto Welldone;
:Welldone
echo cool
pause
Simple code.
As soon as I answer Q1with the correct answer "Sundar Pichai", the bat file closes.
What am I doing wrong? How can I fix it?
Please help me!
Edit: If I type "SundarPichai" without spaces, it works.
But how do I make it work with spaces?

"set/a was unexpected at this time"

I'm new to batch and am mainly using it for this one school project. I'm also new to stack overflow, so leniency of mistakes would be appreciated. I keep getting the error message "set/a was unexpected at this time" when I get to ":MARK" in the code. I'm just going to dump the entire code here, sorry if it's confusing.
#echo off
color 7C
:MAINMENU
cls
title Main Menu
echo To start the quiz, type 1
echo To view the accreditations, type 2
echo To exit the Shop Quiz, type 3
set/p menuselection= Make your selection:
if %menuselection%==1 goto QUESTIONONE
if %menuselection%==2 goto ACCREDITATION
if %menuselection%==3 goto ENDPROGRAM
:QUESTIONONE
cls
title Question One of Ten
echo What do you do if there is an accident in the shop?
echo.
echo 1. Duck and cover your head to avoid injury
echo 2. Immediately go tell the instructor
echo 3. Walk it off
echo 4. Help the person who got hurt and fix the problem
echo.
set/p aone=
goto QUESTIONTWO
:QUESTIONTWO
cls
title Question Two of Ten
echo What attitude should you always have in the shop?
echo.
echo 1. Calm and careful
echo 2. Focused on surroundings
echo 3. Mischievous and goofy
echo 4. Attentive and ready to avoid injury
echo.
set/p atwo=
goto QUESTIONTHREE
:QUESTIONTHREE
cls
title Question Three of Ten
echo Which of the following is NOT allowed for dress in shop?
echo.
echo 1. Sandals
echo 2. Tucked Loose clothing
echo 3. Crocs
echo 4. Eye Goggles
echo.
set/p athree=
goto QUESTIONFOUR
:QUESTIONFOUR
cls
title Question Four of Ten
echo What list of items must you be able to locate in the shop?
echo.
echo 1. Wood, tool shelf, eye goggles, fire extinguishers, power buttons
echo 2. Fire blankets, first aid kits, fire extinguishers, power buttons, eye goggles
echo 3. Eyewash stations, first aid kits, fire extinguishers, fire blankets, power buttons
echo 4. Eye goggles, fire blankets, tool shelf, fire extinguishers, power buttons
echo.
set/p afour=
goto QUESTIONFIVE
:QUESTIONFIVE
cls
title Question Five of Ten
echo Which of these potential risks is caused by clutter and spills?
echo.
echo 1. Bumping into things
echo 2. Unnecessary strain
echo 3. The drill catching the stock and spinning it
echo 4. Prevention of proper lifting techniques
echo.
set/p afive=
goto QUESTIONSIX
:QUESTIONSIX
cls
title Question Six of Ten
echo Which of these IS an acceptable mental condition for shop work?
echo.
echo 1. Tired
echo 2. Hesitant
echo 3. Rushed
echo 4. Under the influence
echo.
set/p asix=
goto QUESTIONSEVEN
:QUESTIONSEVEN
cls
title Question Seven of Ten
echo Your work station should NOT be:
echo.
echo 1. Adequately ventilated
echo 2. Well-organized
echo 3. Compact
echo 4. Well-lit
echo.
set/p aseven=
goto QUESTIONEIGHT
:QUESTIONEIGHT
cls
title Question Eight of Ten
echo What tool should you use to secure your work?
echo.
echo 1. Wrench
echo 2. Paper weight
echo 3. Vice
echo 4. Your hand
echo.
set/p aeight=
goto QUESTIONNINE
:QUESTIONNINE
cls
title Question Nine of Ten
echo What can you use to hold onto things like nails, screws, or chisels?
echo.
echo 1. Pocket
echo 2. Hands
echo 3. Mouth
echo 4. All of the above
echo.
set/p anine=
goto QUESTIONTEN
:QUESTIONTEN
cls
title Question Ten of Ten
echo Of the following only one action is acceptable. Select it.
echo.
echo 1. Using files without handles
echo 2. Prying with a chisel
echo 3. Using clamps that are really big
echo 4. Hammering with a wrench
echo.
set/p athree=
goto RESULTS
:RESULTS
cls
title Results
goto MARK
:RESULTONE
echo Question one of ten
echo What do you do if there is an accident in the shop?
if %aone%==1 echo "Duck and cover your head to avoid injury" is wrong.
if %aone%==1 echo Correct answer is 2, "Immediately go tell the instructor"
if %aone%==2 echo "Immediately go tell the instructor" is correct.
if %aone%==3 echo "Walk it off" is wrong.
if %aone%==3 echo Correct answer is 2, "Immediately go tell the instructor"
if %aone%==4 echo "Help the person who got hurt and fix the problem" is wrong.
if %aone%==4 echo Correct answer is 2, "Immediately go tell the instructor"
pause
goto RESULTTWO
:RESULTTWO
echo Question two of ten
echo What attitude should you always have in shop?
if %atwo%==1 echo "Calm and careful" is correct.
if %atwo%==2 echo "Focused on surroungings" is wrong.
if %atwo%==2 echo Correct answer is 1, "Calm and careful"
if %atwo%==3 echo "Mischievous" is wrong.
if %atwo%==3 echo Correct answer is 1, "Calm and careful"
if %atwo%==4 echo "Attentive and ready to avoid" is wrong.
if %atwo%==4 echo Correct answer is 1, "Calm and careful"
pause
goto RESULTTHREE
:RESULTTHREE
echo Question three of ten
echo Which of the following is NOT allowed for dress in shop?
if %athree%==1 echo "Sandals" is correct.
if %athree%==2 echo "Tucked loose clothing" is wrong.
if %athree%==2 echo Correct answer is 1, "Sandals"
if %athree%==3 echo "Crocs" is wrong.
if %athree%==3 echo Correct answer is 1, "Sandals"
if %athree%==4 echo "Eye goggles" is wrong.
if %athree%==4 echo Correct answer is 1, "Sandals"
pause
goto RESULTFOUR
:RESULTFOUR
echo Question four of ten
echo What list of items must you be able to locate in the shop?
if %afour%==1 echo "Wood, tool shelf, eye goggles, fire extinguishers, power buttons" is wrong.
if %afour%==1 echo Correct answer is 3, "Eyewash stations, first aid kits, fire extinguishers, fire blankets, power buttons"
if %afour%==2 echo "Fire blankets, first aid kits, fire extinguishers, power buttons" is wrong.
if %afour%==2 echo Correct answer is 3, "Eyewash stations, first aid kits, fire extinguishers, fire blankets, power buttons"
if %afour%==3 echo "Eyewash stations, first aid kits, fire extinguishers, fire blankets, power buttons" is correct.
if %afour%==4 echo "Eye goggles, fire blankets, tool shelf, fire extinguishers, power buttons" is wrong.
if %afour%==4 echo Correct answer is 3, "Eyewash stations, first aid kits, fire extinguishers, fire blankets, power buttons"
pause
goto RESULTFIVE
:RESULTFIVE
echo Question five of ten
echo Which of these potential risks is caused by clutter and spills
if %afive%==1 echo "Bumping into things" is correct.
if %afive%==2 echo "Unnecessary strain" is wrong.
if %afive%==2 echo Correct answer is 1, "Bumping into things"
if %afive%==3 echo "The drill catching the stock and spinning it" is wrong.
if %afive%==3 echo Correct answer is 1, "Bumping into things"
if %afive%==4 echo "Prevention of proper lifting techniques" is wrong.
if %afive%==4 echo Correct answer is 1, "Bumping into things"
pause
goto RESULTSIX
:RESULTSIX
echo Question six of ten
echo Which of these is an acceptable mental condition for shop work?
if %asix%==1 echo "Tired" is wrong.
if %asix%==1 echo Correct answer is 2, "Hesitant"
if %asix%==2 echo "Hesitant" is correct.
if %asix%==3 echo "Rushed" is wrong.
if %asix%==3 echo Correct answer is 2, "Hesitant"
if %asix%==4 echo "Under the influence" is wrong.
if %asix%==4 echo Correct answer is 2, "Hesitant"
pause
goto RESULTSEVEN
:RESULTSEVEN
echo Question seven of ten
echo Your work station should NOT be:
if %aseven%==1 echo "Adequately ventilated" is wrong.
if %aseven%==1 echo Correct answer is 3, "Compact"
if %aseven%==2 echo "Well-organized" is wrong.
if %aseven%==2 echo Correct answer is 3, "Compact"
if %aseven%==3 echo "Compact" is correct.
if %aseven%==4 echo "Well-lit" is wrong.
if %aseven%==4 echo Correct answer is 3, "Compact"
pause
goto RESULTEIGHT
:RESULTEIGHT
echo Question eight of ten
echo What tool should you use to secure your work?
if %aeight%==1 echo "Wrench" is wrong.
if %aeight%==1 echo Correct answer is 3, "Vice"
if %aeight%==2 echo "Paper weight" is wrong.
if %aeight%==2 echo Correct answer is 3, "Vice"
if %aeight%==3 echo "Vice" is correct.
if %aeight%==4 echo "Your hand" is wrong.
if %aeight%==4 echo Correct answer is 3, "Vice"
pause
goto RESULTNINE
:RESULTNINE
echo Question nine of ten
echo What can you use to hold onto things like nails, screws, or chisels?
if %anine%==1 echo "Pocket" is wrong.
if %anine%==1 echo Correct answer is 2, "Hands"
if %anine%==2 echo "Hands" is correct.
if %anine%==3 echo "Mouth" is wrong.
if %anine%==3 echo Correct answer is 2, "Hands"
if %anine%==4 echo "All of the above" is wrong.
if %anine%==4 echo Correct answer is 2, "Hands"
pause
goto RESULTTEN
:RESULTTEN
echo Question ten of ten
echo Of the following only one action is acceptable. Select it.
if %aten%==1 echo "Using files without handles" is wrong.
if %aten%==1 echo Correct answer is 3, "Using clamps that are really big"
if %aten%==2 echo "Prying with a chisel" is wrong.
if %aten%==2 echo Correct answer is 3, "Using clamps that are really big"
if %aten%==3 echo "Using clamps that are really big" is correct.
if %aten%==4 echo "Hammering with a wrench" is wrong.
if %aten%==4 echo Correct answer is 3, "Using clamps that are really big""
pause
goto MAINMENU
:MARK
set mark= 0
if %aone%==2 set/a mark="%mark%+1"
if %atwo%==1 set/a mark="%mark%+1"
if %athree%==1 set/a mark="%mark%+1"
if %afour%==3 set/a mark="%mark%+1"
if %afive%==1 set/a mark="%mark%+1"
if %asix%==2 set/a mark="%mark%+1"
if %aseven%==3 set/a mark="%mark%+1"
if %aeight%==3 set/a mark="%mark%+1"
if %anine%==2 set/a mark="%mark%+1"
if %aten%==3 set/a mark="%mark%+1"
echo %mark%"/10" questions correct.
echo.
echo To see your answers, type 1
echo To go to the main menu, type 2
echo To exit the quiz, type 3
set/p markselection= Make your selection:
if %markselection%==1 goto RESULTONE
if %markselection%==2 goto MAINMENU
if %markselection%==3 goto ENDPROGRAM
:ACCREDITATION
cls
title Accreditation
echo -Information taken from Grade 9 shop question booklet
echo -Software used: Zed (available on chrome web store),
echo Notepad++ (available by google search),
echo Google drive and related services (available online)
echo -Creator: Exalor Stepaniuk (shop class grade censored, 2016/17)
echo.
pause
goto MAINMENU
:ENDPROGRAM
exit
Again, sorry if I did something wrong by dumping the entire script here. I just don't really know what i'm doing.
The root cause of your bug is this line in QUESTIONTEN:
set/p athree=
This looks like a typo in that you meant to set aten instead of athree.
More specifically, since aten was empty, within MARK, the comparison ends up being interpreted like this:
if ==3 set/a mark="[mark value]+1"
Notice that the left operand is empty. The line is being interpreted wrong, and that's why you're getting the error. Changing athree to aten will fix this, but there is a trick to fixing comparisons so that you don't break your script when one of your operands is null. Just put something on both sides. For example, surround your comparators by quotes.
if "%leftoperand%"=="%rightoperand%" ...
Then, even if one or both be empty, it won't terminate your script. It will just look like if ""=="".
Also, a general trick to debugging batch files is to narrow down where errors occur (and why) by putting echo statements, even simple ones, like echo 1 at strategic spots. Then, when you see a 1 in the output, you know that line was executed, and your error is after that line. Once you find the bad line that your script is terminating on, you can echo a copy of it to see what the script is interpreting it as, which will help you figure out the problem.
Like this:
echo if %aten%==3 set/a mark="%mark%+1"
if %aten%==3 set/a mark="%mark%+1"
I hope that helps.

Endless loop in batch file using 'goto'

Well, I have this problem with a little program that I'm making because my loop is endless. Right now I'm trying to use goto command but it doesn't work couse it is endless. Anyone please help.
#echo off
color 1a
:i
echo Hi, mate Let's start off by getting each others names.
timeout 5 > nul
echo What is your name ?:
set /p name=
echo Hi noob %name% my name is Youke
timeout 2 > nul
echo %name% We are going on an advenutre quest today :D
timeout 2 > nul
goto i
Somewhere between the i label on line 3 and the goto command, you need to add code that will break out of the loop—likely an if statement with another goto inside it that points to a label after your current goto.
You can see plenty of examples here: https://stackoverflow.com/a/4711984/436282

How would I make Else work in Batch?

So, I'm playing around with Batch a little bit and I've encountered a problem that I think you guys could help with. So I'm trying to make a thing where you can answer Yes or No on a question, and if you don't answer Yes nor No then it'll tell you that it wasn't a valid answer. You can take a look at the code right here.
:Choice
set /p Choice=Is a stone a stone?
if %Choice%==Yes goto Correct
if %Choice%==No goto Incorrect
else goto Invalid
:Correct
echo You are correct!
pause
goto end
:Incorrect
echo You are incorrect!
pause
goto end
:Invalid
echo You did not enter a valid answer.
pause
goto end
:end
exit
So I cannot use Else in the way I've done in the code, but do you know how I would do the same thing as Else would do IF it worked? May sound a little confusing, I hope you understand. Just to clarify, here's the bit I don't know how to do:
else goto Invalid
Just leave out the word else, and have an unconditional Goto after your two if statements.
set /p Choice=Is a stone a stone?
if %Choice%==Yes goto Correct
if %Choice%==No goto Incorrect
goto Invalid
David's answer is correct. I only add this answer (using your code) to show you that ELSE does exist and how you might use it. Note that I use quotes on strings in case there are spaces or special characters in variable... or in case the variable is blank. I also used /i to specify a case insensitive compare. Note that the ) else ( has to be on the same line. It could have been on the same line as the line above it; or all 3 of those lines could be on 1 line.
:Choice
set /p Choice=Is a stone a stone?
if /i "%Choice%"=="Yes" goto Correct
if /i "%Choice%"=="No" (goto Incorrect
) else (
goto Invalid)
:Correct
echo You are correct!
goto end
:Incorrect
echo You are incorrect!
goto end
:Invalid
echo You did not enter a valid answer.
pause
goto :Choice
:end
pause
exit
Correcting answer: You can find information about using ELSE in batch below. However, David's answer is more elegant:
Source: http://ss64.com/nt/else.html

Resources