Unexpected exit after user input - batch-file

Need a little help here for my code.
When the script detects save-file it needs to ask whether to re-create it. At this point, I click on Y for Yes, but it exits.
Here's the code:
:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
echo Please enter (Y,N):
set /p choice=
if %choice% == "y" goto welcome
if %choice% == "n" exit
if %choice% == "Y" goto recreation
if %choice% == "N" exit

Use quotes on both sides, here is an example:
BAD:
:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
echo Please enter (Y,N):
set /p choice=
if %choice% == "y" goto welcome
if %choice% == "n" exit
if %choice% == "Y" goto recreation
if %choice% == "N" exit
GOOD:
:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
echo Please enter (Y,N):
set /p choice=
if "%choice%" == "y" goto welcome
if "%choice%" == "n" exit
if "%choice%" == "Y" goto recreation
if "%choice%" == "N" exit
Also here I have made a better version of your code:
:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
choice /c ynY /cs /m "It appears that you already created the configuration file, would you like to save it"
cls
if "%errorlevel%" == "1" goto welcome
if "%errorlevel%" == "2" exit
if "%errorlevel%" == "3" goto recreation

When you are including quotes only in one side, then the quotes are included in the comparison. I wouldn't recommend neither removing the quotes from one side, which isn't safe, nor adding quotes to both sides like this (which also works good) [parenthesis included]:
:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
echo Please enter (Y,N):
set /p choice=
if "%choice%" == "y" (goto welcome)
if "%choice%" == "n" (exit)
if "%choice%" == "Y" (goto recreation)
if "%choice%" == "N" (exit)
Instead, I would go forward and entirely change your code using the choice command doing the following:
:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
choice /C:YyNn /CS /M "Please enter (Y,N):" /N
if errorlevel 3 (exit)
if errorlevel 2 (goto :welcome)
if errorlevel 1 (goto :recreation)
which is much sorter.
For more info about the commands used, open a new cmd and type:
echo /?
choice /?
if /?

Related

In batch my menu will not work

I was trying to create a simple menu system with batch but i can't tell if it's the 'goto' or the input part that i messed up on. help will be appreciated!
#echo off
cls
echo ==============MENU==============
echo 1.
echo 2.
echo 3.
echo choose.
set/p "menuInput"
if %menuInput%==1 (goto :1)
if %menuInput%==2 (goto :2)
if %menuInput%==3 (goto :3)
else echo error
:1 echo 1
:2 echo 2
:3 echo 3
The syntax of set /p is incorrect. Use set /? to read the help.
The parentheses around (goto :1) don't buy you anything.
The else is both syntactically incorrect (must be on the same logical line as the if) and useless (because if the if had succeeded control would have been passed to :3).
You are missing a bunch of exit /b or goto :eof in order to prevent control falling through the options.
ok i updated your code now this shold work
code here
#echo off
:menu
cls
title menu
echo ==============MENU==============
echo 1.
echo 2.
echo 3.
set /p menuInput=? 1-3
if %menuInput% EQU 1 goto 1
if %menuInput% EQU 2 goto 2
if %menuInput% EQU 3 goto 3
goto error
:1
echo1
pause
:2
echo2
pause
:3
echo3
pause
:error
echo error press 1-3
pause>nul
goto menu
As mentioned by #AlexP,your syntax of set /p is incorrect
you need to use set < space > /p < space > userinput=
and not set/p and there should be = sign after your variable name
SET /P variable=[promptString]
Read More about using SET & IF ELSE and also CHOICE
(edit- removed misleading code for error check)
sample batch with menu using Choice and SET /P IF Else while choosing option from menu.
Example 1 using IF ELSE (input error check)
#echo off
::Your Menu to choose from
:menu
cls
echo ==============MENU==============
echo 1.command menu item 1
echo 2.command menu item 2
echo 3.command menu item 3
echo.
set /p userinput=
if %userinput%==1 (
goto 1
) else if %userinput%==2 (
goto 2
) else if %userinput%==3 (
goto 3
) else (
goto error
)
:1
echo command 1
pause>nul
goto menu
:2
echo command 2
pause>nul
goto menu
:3
echo command 3
pause>nul
goto menu
::Incorrect Input go back to menu
:error
echo Incorrect User Input
pause>nul
goto menu
:end
exit
Example 2 using CHOICE command (no error input check)
#echo off
::Your Menu to choose from
:menu
cls
echo ==============MENU==============
echo 1.command menu item 1
echo 2.command menu item 2
echo 3.command menu item 3
::check and limit user input to selection
choice /C 123 /N /M "Your Selection"
IF "%ERRORLEVEL%"=="1" goto 1
IF "%ERRORLEVEL%"=="2" goto 2
IF "%ERRORLEVEL%"=="3" goto 3
:1
echo command 1
pause>nul
goto menu
:2
echo command 2
pause>nul
goto menu
:3
echo command 3
pause>nul
goto menu
Wrong is set /p.
try set /p menuInput =

Batch Script: Using GOTO on IF statement

When you get to the user input part, no matter what I type (desk, fire, door) it always goes to fireplace. Is there something wrong with my if syntax?
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if %choice%=="fire" GOTO fireplace
if %choice%=="desk" GOTO desk
if %choice%=="door" GOTO door
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
:desk
echo.
echo You go to the desk.
echo.
:door
echo.
echo You go to the door.
echo.
Doublequote %choice% or it will not be equal: desk is not equal as "desk".
And exit your Label block with a goto:eof or exit/b.
Use the /i switch with IF so you can also use DESK or DesK
if /i "%choice%"=="fire" GOTO fireplace
if /i "%choice%"=="desk" GOTO desk
if /i "%choice%"=="door" GOTO door
goto:error
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
exit/b
:desk
echo.
echo You go to the desk.
echo.
exit/b
:door
echo.
echo You go to the door.
echo.
exit/b
You need to protect the entry points of each of your target blocks, otherwise when it's done executing the target block, it will "fall into" the next block.
Before any label line (e.g., :fireplace), you'll need a GOTO to make sure that the program flow doesn't "fall into" the next routine:
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if /I "%choice%" EQU "fire" GOTO fireplace
if /I "%choice%" EQU "desk" GOTO desk
if /I "%choice%" EQU "door" GOTO door
GOTO END
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
GOTO END
:desk
echo.
echo You go to the desk.
echo.
GOTO END
:door
echo.
echo You go to the door.
echo.
:END
Note also the changes to the IF statements. These allow you to handle the case of typing FIRE or Fire instead of just fire.
In addition to the tips from Jeff and SachaDee,
if you name all your labels equal to the choice you could use a loop
For %%A in (fire desk door) Do If /i "%choice%" equ "%%A" Goto %choice%
Or with a llimited number of choices you could use choice.exe and work with a single letter answer (no enter needed then) and evaluate the returned errorlevel.

choice command in Batch not working

Okay, so I'm having some trouble using errorlevel and the choice command, and frankly I have no idea what's going on.
I've been using this code:
:CACD
set stage=CACD
echo.
echo Make a choice
echo.
echo 1)
echo 2)
echo 3)
echo.
choice /c 7034 /n
if %errorlevel% == "3" goto choice3
if %errorlevel% == "2" goto se2
if %errorlevel% == "1" goto choice1
goto CACD
:choice3
echo you chose 3
pause
goto CACD
:se2
echo you chose 2
pause
goto CACD
:choice1
echo you chose 1
goto CACD
Whenever I enter 1, nothing happens. Same thing with 2. But whenever I enter 3, it works? Can anyone help?
Since choice limits your input, errorlevel can only be one of 1,2,3,255
You can omit all the if commands if you append the errorlevel to your goto label:
and name all the labels accordingly.
#Echo off
:CACD
set stage=CACD
echo.
echo Make a choice
echo.
echo 1)
echo 2)
echo 3)
echo.
choice /c 123 /n
goto choice%errorlevel%
:Choice255
Echo an error occured with your choice
goto :Eof
:choice3
echo you chose 3
pause
goto CACD
:choice2
echo you chose 2
pause
goto CACD
:choice1
echo you chose 1
goto CACD

goto unexpected at this time error for game test

When I run this, it keeps saying "goto" unexpected at this time. I have tried to solve the issue, but it won't work.
Here is the code:
#echo off
color 0a
title TEST FOR HACK SIM
:menu
ECHO what do you want to test?
echo.
echo -open disk
echo -save save
echo -open save
echo.
echo.
set /p choice= choose one:
if %choice% == "open disk" goto disk
if %choice% == "open save" goto saveo
if %choice% == "save save" goto saves
:disk
call D:\
echo did it work!?
goto menu
:saveo
cls
pause
(
set /p save=
)<C:\Users\%username%\Searches\savefiles.txt
echo %save%
pause
goto menu
:saves
set /p fun= what number do you want?>
(
echo %fun%
)>C:\Users\%username%\Searches\savefiles.txt

Batch opening wrong file type

So here's my ENTIRE code:
#echo off
cls
color fc
:Start
cls
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
set/p t=Try Again? (Y/N)
if %t%==Y goto Start
if %t%==y goto Start
if %t%==N goto Quit
if %t%==n goto Quit
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
set/p g=Choice:
if %g%== 1 goto KSPEnd
:KSPEnd
start "" "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP.exe"
cls
goto Quit
:Quit
cls
echo Goodbye
Timeout 1
But the code opens the .exe AND a .txt file with exactly the same name. I can't rename the files. So basically i'm asking how to open a specific file type.
Thanks
Instead of starting C:\....\KSP.exe, first go to the right directory, then start KSP:
cd "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program"
KSP.exe
Ok I've got two things for you. Firstly I'll give you you desired solution.
Treat it like an operatable program
rem To start Kerbal Space Program:
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
Thats it. Really.
Secondly:
Use the Choice command
you keep on using set /p where choice would be much better.
Just to be convenient I redid you code with everything I would do. Have fun!
Code :
#echo off
cls
color fc
title -{GAMELOADER}-
:Start
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
set Er=Userid
goto :Crash
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
choice /c yn /m "Try Again?"
if %errorlevel%==1 goto Start
if %errorlevel%==2 goto Quit
set Er=Loop-End_of_stream
goto :Crash
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
set Er=Passid
goto :Crash
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
echo.
Choice /c 1 /m "Game: "
if %errorlevel%==1 goto KSPEnd
set Er=Gameid
goto :Crash
:KSPEnd
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
goto Quit
set Er=End_of_file___UNKNOWN
goto :Crash
:Quit
cls
echo Goodbye
Timeout 1
Exit
:Crash
Rem Always useful :)
Echo Program has crashed Error: %Er%
Pause
Exit
Hope that helped. Mona

Resources