Batch: goto was unexpected at this time error - batch-file

So im making a project for school and i want to goto menu if the user input is not valid
My code is this:
:menu
echo.
echo.
echo What would you like to know first?
echo.
echo.
echo 1) Salary
echo 2) What Do they do?
echo 3) Schools near me that offer game design programs
echo 4) Requirements
echo 5) Credits
echo.
echo.
set /p choice="Enter Number: "
if %choice% == 1 goto salary
if %choice% == 2 goto do
if %choice% == 3 goto schools
if %choice% == 4 goto req
if %choice% == 5 goto credits
goto menu
when i enter input, there is a line that flashes
Goto was unexpected at this time then exits
What am i doing wrong?
I should add that my input is not a valid option. (1,2,3,4,5)

if %choice% == 1 goto salary
Imagine, what this would do, when %choice% is empty. The line would then be read as:
if == 1 goto salary
Obviously a syntax error. "goto was unexpected".
To avoid this, use
if "%choice%" == "1" goto salary
This would be read as:
if "" == "1" goto salary
Syntax is ok now.

Related

batch - How to return error when blank input on set /p

I have a section of my batch file which prompts the end user to enter a case insensitive Y or N to continue.
But if I just press ENTER, the batch file quits.
I want it to return an error, when the person inputs blank or anything other than y/Y/n/N.
Here's my code:
I solved it by simply adding
if %errorLevel% == 1 goto loserrr2
and
if not %heckerz% == y goto loserrr2
if not %heckerz% == n goto loserrr2
if not %heckerz% == Y goto loserrr2
if not %heckerz% == N goto loserrr2
thanks to Jerry Jeremiah for the help :D
and sorry for any error i commited on my question, as i said im new to Stack OverFlow
Instead of set /p try using choice:
%windir%\System32\choice.exe /m "Start the heckerz show ?" /c YN
if %errorlevel%==1 goto goodStuff
if %errorlevel%==2 goto loserrr
Hmm...
so maybe this is the answer:
:heckerz
#echo off
title Heckerz Tools
#echo Text
set /P "opcao=Start the heckerz show? (Y/N): "
if '%opcao%' equ 'y' goto goodStuff
if '%opcao%' equ 'n' goto loserrr
if '%opcao%' equ 'Y' goto goodStuff
if '%opcao%' equ 'N' goto loserrr
goto heckerz
:goodStuff
#echo Text 1
exit
:loserrr
#echo Text 2
exit
Also please don't upload a photo when asking a question

How To Set Variable Value In If

Hi I make simple batch file now what I tried to add correct answers number at the end but it keeps saying zero because the variable values are not changing when the answers are chosen. Here is my code below
#echo off
title Game One
color 1f
::#############################
:one
set correctn=0
set correctn2=0
cls
echo What is 2 + 2?
echo.
echo.
echo A) 6
echo B) 4
echo C) 49
echo D) 17
echo.
echo.
echo Type the correct answer.
set /p ch1=
echo.
echo.
if not defined ch1 (goto one)
if %ch1%==A goto no
if %ch1%==A correctn=0
if %ch1%==B goto yes
if %ch1%==B correctn=1
if %ch1%==C goto no
if %ch1%==C correctn=0
if %ch1%==D goto no
if %ch1%==D correctn=0
pause>null
::#########################################
:no
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 4.
pause>null
goto two
::#########################################
:yes
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto two
::##########################################
:two
cls
echo What is 100 divided by 2?
echo A) 45
echo B) 50
echo C) 90
echo D) 17
echo.
echo.
set/p ch2=
echo.
echo.
if not defined ch2 (goto two)
if %ch2%==A goto no2
if %ch2%==A correctn2=0
if %ch2%==B goto yes2
if %ch2%==B correctn2=1
if %ch2%==C goto no2
if %ch2%==C correctn2=0
if %ch2%==D goto no2
if %ch2%==D correctn2=0
echo Invalid Choice, Please Try Again!
pause>null
::#################################
:no2
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 50.
pause>null
::########################################
:yes2
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto end
::######################################
:end
set/a correct=%correctn% + %correctn2%
echo Number of answers correct was %correct%
pause>null
So how to change variable value in if statement if already variable exists?
You need to set a variable first and then goto. As currently written, your script goes to a label yes first. Hence, if %ch1%==B set correctn=1 line is never reached:
if %ch1%==B goto yes
if %ch1%==B set correctn=1
if %ch1%==B goto yes
Moreover, IF command string comparison is case sensitive without /I switch; try the following:
if /I %ch1%==B (set correctn=1&goto yes)
I'd suggest using (Windows native) CHOICE.EXE instead of set /p for a single key-press user input, e.g as follows:
CHOICE /C abcd
IF %errorlevel%==2 (
set correctn=1
goto yes
) else (
set correctn=0
goto no
)

Batch-file wrong input fix?

#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
If /i "%INPUT%" == "Jonah" goto Jonah
If /i "%INPUT%" == "" goto Wrong
I have a strange problem. Above is a code and for some reason, when I type in "INPUT" something like 'Aiden' it would think it meant '1'. Is there a way to make every wrong answer goto wrong. (Wrong answers like Aiden which isn't even specified there. But not only Aiden, any other thing).
After your if statements you need to put a catch-all GOTO statement so if none of the others work, it will go to wrong instead of just continuing into the block right below
#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
If /i "%INPUT%" == "Jonah" goto Jonah
goto Wrong

Assistance with nested if statements

I'm new to stackOverflow. I have a question. Why is my code not working...it's nested if statements, that should work...I'm stumped. You are supposed to have money, a home (type 1 to 3) and phone. The phone is the problem. callLine is used to tell which line will be displayed each time callText is called. It adds 1 to itself every time it is called in Calling2. Whenever it gets to a certain number, it changes callText (the text displayed) to something else. But with callWho variable (work or home) it needs to be checked aswell. Here it is:
# echo off
set money=10000
set house=1
set item=Phone
set /a callLine=0
setlocal enabledelayedexpansion
echo Hello there! I am Joe, and I am the mayor of Redcrest Town.
pause
cls
echo I'm so happy to see another resident move into the Redcrest Town.
echo So, will you do me a favour and tell me your name?
set /p Name=
cls
echo Well then, %name%! Welcome!
pause
cls
echo I hope you have a great time here. You have 10,000 dollars, and a small house.
echo Please enjoy your time here!
pause
cls
:UsePhone
echo Money: %money%
echo.
echo You are using your phone. This will cost money, each time you call.
echo.
echo.
echo __i [CONTACT LIST]
echo ^|---^| [1: HOME ]
echo ^|[_]^| [2: WORK ]
echo ^|:::^|
echo ^|:::^|
echo `\ \
echo \_=_\
set /p callWho=Select Number:
if %callWho% == 2 goto Calling
echo %callText%
pause
:Calling
cls
echo Calling......
ping localhost -n 4 >nul
:Calling2
if callLine == 0 if %callWho% == 2 set callText="Hello? Is this %name%?"
if callLine == 1 if %callWho% == 2 set callText="Oh hello! It's really nice to talk to you again."
if callLine == 2 if %callWho% == 2 set callText="Well, I will see you soon, %name%! Bye!"
if callLine == 3 if %callWho% == 2 set callText="END"
set /a callLine = callLine + 1
cls
echo __i
echo ^|---^|
echo ^|[_]^| PHONE: %callText%
echo ^|:::^|
echo ^|:::^|
echo `\ \
echo \_=_\
ping localhost -n 5 >nul
if %callText% == "END" goto UsePhone
goto Calling2
I keep crashing with: . is unexpected at this time....
Really unusual.
You forgot the %% around callLine.
:Calling2
if %callLine% == 0 if %callWho% == 2 set callText="Hello? Is this %name%?"
if %callLine% == 1 if %callWho% == 2 set callText="Oh hello! It's really nice to talk to you again."
if %callLine% == 2 if %callWho% == 2 set callText="Well, I will see you soon, %name%! Bye!"
if %callLine% == 3 if %callWho% == 2 set callText="END"
set /a callLine = callLine + 1
When testing variables with if statements, the variables must be enclosed in % %

Goto was unexpected at this time?

I am trying to make a little connect four game and whenever I hit enter without typing anything into the console it says that goto was unexpected at this time All I want it to dos go back to :X for now if there is no user input. Any help would be aappreciated.
:X
cls
echo --------------
echo Connect Four X
echo --------------
echo.
echo CHOOSE A COLUMN
echo.
echo.
echo 1 2 3 4 5 6 7
echo.
echo (%a6%) (%b6%) (%c6%) (%d6%) (%e6%) (%f6%) (%g6%)
echo.
echo (%a5%) (%b5%) (%c5%) (%d5%) (%e5%) (%f5%) (%g5%)
echo.
echo (%a4%) (%b4%) (%c4%) (%d4%) (%e4%) (%f4%) (%g4%)
echo.
echo (%a3%) (%b3%) (%c3%) (%d3%) (%e3%) (%f3%) (%g3%)
echo.
echo (%a2%) (%b2%) (%c2%) (%d2%) (%e2%) (%f2%) (%g2%)
echo.
echo (%a1%) (%b1%) (%c1%) (%d1%) (%e1%) (%f1%) (%g1%)
echo.
echo ---------------------------
echo.
echo.
set /p xchoice=:
if %xchoice% == 1 goto xcheck1
if %xchoice% == 2 goto xcheck2
if %xchoice% == 3 goto xcheck3
if %xchoice% == 4 goto xcheck4
if %xchoice% == 5 goto xcheck5
if %xchoice% == 6 goto xcheck6
if %xchoice% == 7 goto xcheck7
goto X
If you are entering a string with a set/p, then there's no saying that the data entered doesn't contain Spaces. The way to get over that is to "enclose the strings on both sides of the comparison operator in quotes" - that is, double-quotes 'not single quotes'
IF syntax is if string1 operator string2 action.
You need
if "%possiblyemptyvariable%"=="1" goto ...

Resources