ls it possible to have 2 words to compare with IF? - batch-file

#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?

Related

Why does batch script keep exiting after set /P user prompt?

#echo off
cls
echo Hello, there! Today is
date /t
echo and the current time is
time /t
echo I'm so glad to meet you. My name is Hal 10000.
set /p name=What's your name?
cls
echo Well hello, %name%
echo Hey, %name%, & set /p "age"="how old are you in years?"
echo Well that's just great! I'm actually 115 today.
echo I look pretty good, huh!?
echo Well, gotta go, by!
exit
Looks like you need a pause command. Your code is executing correctly but there is nothing stopping the code from exiting before you see your results.
#Echo off
Echo This message will not be displayed.
cls
Echo This message will be displayed.
pause
exit
Adding a pause before the exit will show your results until a button is pressed.

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

Game With The Batch Code Keeps Crashing

#echo off
set SCENARIO21=20
set WEAPON1=gun
set DOG1=buddy
set SCENARIO11=broken laptop
set ALIVE1=yes
:day51
cls
echo.
echo Player 1 %SCENARIO21% %WEAPON1% %SCENARIO11%
echo %DOG1%
echo.
echo.
echo Day 5
echo.
echo That was a close one, im glad i got that jacket off when i did...that could've ended really badly...
pause
cls
echo Dang this road seems to go on forever...
pause
cls
echo Woah...
pause
cls
echo That house looks...normal...
pause
cls
echo Is somebody in there?
pause
cls
echo Come on who would live in that house though...
pause
cls
echo Its old, worn down, the roof is bowed, the paint is peeling...
pause
cls
echo And ew...that furniture looks like something from the 2000s
pause
cls
echo Whoever lived there last must've been an old couple...really old...
pause
cls
echo Maybe there is someone in there
pause
cls
echo The windows look like they're sparkling...
pause
cls
echo Wow. Is that a...?
pause
cls
echo A um...what are those called...Oh yeah...
pause
cls
echo A plant?
pause
cls
echo No...it couldn't be
pause
cls
echo The world's last farmer died about 70 years ago...
pause
cls
echo The 2100s...
pause
cls
echo No one knows how to plant anything, let alone GROW anything anymore...
pause
cls
echo Atleast thats what i thought...
pause
cls
echo I wonder if i should go in...
pause
if %ALIVE1%==yes (
cls
echo Player 1
pause
cls
echo Player 1
echo This house gives me a strange feeling...i can't tell if it's good or bad...
echo -Yes (go in, check the house, maybe it has a planting guide...or maybe theres someone in there...and hey if there is someone, why wouldn't they help you)
echo -No (screw that, its not worth it, if you go in your gonna die, duh...anyways what could be of use in a house as old as the 2000s)
set /p SCENARIO51=Should you go in the house?
if %SCENARIO51%==yes (
cls
echo You walk into the home...
pause
cls
echo Hello?
pause
cls
echo *distant voice* Hello...why are you in my home?
pause
cls
echo Sorry sir...ma'm...um i wasn't trying to bother you...
pause
cls
echo *distant voice* You didn't answer my question...and it's sir, thank you...
pause
cls
echo Oh...i came in because i saw the plants outside...
pause
cls
echo *distant voice* And? Is that your only reason...
pause
cls
echo Well i haven't seen plants until now...i was wondering how?
pause
cls
echo *distant voice* YOU'VE NEVER SEEN PLANTS? THIS IS WHY I GO AGAINST THE SYSTEM...
pause
cls
echo What system sir?
pause
cls
echo *distant voice* If we're going to have a conversation come sit with me...walk to the end of the hall and take a left into my room...
pause
cls
echo You walked into the room of the strange voice and found an old man sitting in a chair that looked like a bubble of water being squished...
pause
cls
echo *Old Man* Im Alexander...im 170 years old...
pause
cls
echo H-h-how?
pause
cls
echo *Alexander* Okay i don't have all day i must attend to other things...I'll tell you what
echo I'll answer 1 question...any question...
pause
cls
if %SCENARIO51%==yes
echo This is your chance...
echo -1 (How did this all start?)
echo -2 (How do i get to the city of Lillian?)
echo -3 (How do i live forever just like you?)
set /p SCENARIOP51=What would you like to ask?
if %SCENARIOP51%==1 (
cls
echo *Alexander* Wow...good question...
echo It started in 2100...
pause
cls
echo When the last farmer died?
pause
cls
echo *Alexander* No, when the last farmer turned immortal...me
pause
cls
echo No i saw it on the news drone...he's dead...
pause
cls
set CODE=LSON5676
echo *Alexander* AGAIN...Sorry, that was the system's news drone...they tend to "confuse" everyone's story to make themselfs look better...
echo Anyways...in 2100 two groups of hacking experts went to war...
pause
cls
echo It ended when one of the groups found a way to literally melt the other groups computers into green radioactive goop...
echo No one really knows for sure how the war was started but most say it was because one group had a hostage...
pause
cls
echo The hostage was said to be the head hacker for the opposing group that was finding some cure...his name was Larson...
echo But for some reason when asking for Larson back the opposing group requested they send %CODE% back...
pause
cls
echo That sounds like a code...
echo better write it down...
echo %CODE%
pause
cls
echo *Alexander* It was all for some stupid reason in my opionion anyways...
echo But back to what happened...
echo After they figured out how to turn the computer into this green goop..they did it to all their computers...
echo Little did they know the goop became radioactive Artificial Intelligence...it could think on its own...
echo And it spread like wild fire...any technology it touched became more green goop...
echo Something else they didn't realize is the goop could sink into your skin with no problem at all...
echo It goes into your veins and overclocks your heart...
echo It was a nano-technology weapon...atomic level capabilities...
echo It was a devastation...
echo And thats all i know...
pause
cls
echo Thank you Alexander...this information won't go to waste...
pause
set ALIVE1=yes
)
if %SCENARIOP51%==2 (
cls
set CODE=5676
echo *Alexander* HA! thats simple...
echo you're not that far either...
echo Go down the road and take the 3rd right
echo You'll be right in front of it...
pause
cls
echo You followed his advice and fell into a trap
pause
cls
type "the end.txt"
pause
cls
set ALIVE1=no
)
if %SCENARIOP51%==3 (
cls
echo *Alexander* Come with me ill show you...
echo This guy seems kinda fishy...
echo Go (go with Alexander to find out what makes him live forever)
echo Leave (Don't follow Alexander, say you have a group waiting outside and you need to get back to them...)
)
if %SCENARIOP51%==3 (
set /p SCENARIOPP51=Do you want to follow Alexander?
)
if %SCENARIOP51%==3 (
if %SCENARIOPP51%==go (
cls
set CODE=LSON5676
echo *Alexander* you aren't smartest are you?
pause
cls
echo You turn around and...
pause
cls
echo *BANG*
pause
cls
echo You died...
pause
cls
type "the end.txt"
pause
set ALIVE1=no
)
if %SCENARIOPP51%==leave (
cls
set CODE=LSON5676
echo *Alexander* I know you're not with anyone but that dog of yours %DOG1%...
pause
cls
echo How do you know his name?
pause
cls
echo *Alexander* you called him to come as you walked down the street cause he was sniffing something...i over heard...
echo I respect your efforts to not fall into traps though...
echo Take this...
pause
cls
echo Alexander handed you a small square of yellow paper with the writing %CODE% on it...
pause
cls
echo *Alexander* take the sticky note and get out...
pause
cls
echo You take the sticky note and leave the house...
pause
cls
echo Whats a sticky note?
pause
)
)
if %SCENARIO51%==no (
cls
echo That thing gives me the creeps...
echo Bye bye house of horror...
set SCENARIO51=none
pause
set ALIVE1=yes
)
)
)
set END=0
if %ALIVE1%==no (
set /a END=%END%+1
)
if %END%==1 (
exit
)
goto day61
:day61
echo yay!
pause
exit
I've tried everything, im very new to coding hence the simple codes but im so confused on what's wrong. I've been coding this 4 player game for about 4 days and i've been stuck trying to fix this specific code. Whenever i load up the game I can get here and get through each scenario but then for instance if i chose to ask the first question I could get through all the dialogue BUT the %CODE% doesn't show up because #echo is off. Also, after completing the 1st question it will still ask me this part:
if %SCENARIOP51%==3 (
cls
echo *Alexander* Come with me ill show you...
echo This guy seems kinda fishy...
echo Go (go with Alexander to find out what makes him live forever)
echo Leave (Don't follow Alexander, say you have a group waiting outside and you need to get back to them...)
)
if %SCENARIOP51%==3 (
set /p SCENARIOPP51=Do you want to follow Alexander?
)
Even though %SCENARIOP51%==1 it still has me set /p SCENARIOPP51 even though %SCENARIOP51% doesn't equal 3. Please Help It Would Be Veryyyyyy Appreciated. Thanks! :)
if %ALIVE1%==yes (
cls
echo Player 1
pause
cls
echo Player 1
echo This house gives me a strange feeling...i can't tell if it's good or bad...
echo -Yes (go in, check the house, maybe it has a planting guide...or maybe theres someone in there...and hey if there is someone, why wouldn't they help you)
Note that this fortuitous? ) ends the if. If you want to actually show the ) here then you need to escape the ) by coding ^). The drawback though is that your if is then not closed for a large number of lines (I'm not going to look through all that code) - and your if %SCENARIO51% opens (another) nested code-block.
Within a code-block (a parenthesised sequence of lines) any %var% is replaced by the value of that variable when the instruction controlling the block (if, for) is encountered. Hence, your set/p which varies SCENARIO51 will appear to use the previous value of SCENARIO51 for %SCENARIO51% within the block.
You need to look at delayedexpansion - hundreds of SO articles about that.
And also be warned : replying Enter alone to a set/p will leave the variable unchanged.
Since you are using set/p you should also use the format
if "%varname%"=="targetstring" ...
in order to have some immunity to empty/separator-containing strings.
Further, the /i switch applied to if will make the comparison case-insensitive.
And it may benefit you to learn batch subroutines. See call /? from the prompt for documentation.

If statements not working in 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)

I need a way to prompt a user for an input using batch

I noticed a post on how to make a batch that opens another batch upon an input. However, I need a way that will make the code pause until the correct input is entered, then continue.
So for example, the code would ask what is the access code?
Then the user would input the correct code, for example 123.
Then the code would say "Welcome!"
Then it would execute another question like "What do you want to do today?"
There would be a list of options:
A. Game
B. Browse
C. Nevermind
The user would in put a, b, or c and the script would start either a game or web browser. If the user selected C, then it would exit.
Thanks for the help!
#echo off
echo Welcome!
echo What do you want to do today?
echo.
echo A. Game
echo.
echo B. Browse
echo.
echo C. Nevermind
echo.
choice /C:ABC /N /M "Enter your choice: "
if "%errorlevel%"=="1" goto :game
if "%errorlevel%"=="2" goto :browse
if "%errorlevel%"=="3" goto :nevermind
goto :error
I think a little bit modified version of code should work just fine.
#Echo off
:Start
cls
echo Welcome !
echo To Greet press one.
echo For Goodbye press two.
echo To Abort press 3
ECHO.
ECHO.
SET /p Option=Choice:
if "%Option%"=="1" GOTO Sub_MenuA
if "%Option%"=="2" GOTO Sub_MenuB
if "%Option%"=="3" GOTO Sub_MenuC
if "%Option%"=="quit" GOTO EOF
Goto Start
:Sub_MenuA
echo Hi There!
pause
Goto Start
:Sub_MenuB
echo tatas !
pause
Goto Start
:Sub_MenuC
echo Aborted
Pause
Goto Start
:EOF

Resources