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
Related
I have a batch-game where there's some questions for one person in particular, I always change the file when she(the person) isn't nearby so its kind of a surprise.
The thing is I keep on displaying questions of different types but sometimes the GOTO or IF statements just simply doesn't work properly, it just follow the line or exit the file.
#ECHO OFF
setlocal EnableDelayedExpansion
ECHO.
ECHO "i just skipped all this echo for you"
pause>nul
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
cls
cd "-somewhere in my pc-"
start "-the file-"
cls
goto :somewhere1
:somewhere1
cls
echo.
echo "skip"
pause>NUL
goto :somewhere2
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
:correct
cls
echo.
echo "skip"
echo.
echo enter to exit
pause>nul
exit
:somewhere_else1
cls
echo.
echo bad answer
pause>nul
goto :somewhere2
Here are my problems:
When it ask's you the question
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
it's just work properly, it wasn't working in the past but I magically fixed it with no idea what i'm doing.
but then I just wanted to make it bigger and add the next lines:
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
and now the file is executing perfectly the ":correct" part but no the ":somewhere_else1". in the case of ":somewhere_else1" its just exit the file. in the past was exiting the file the right one too but again I fixed it just writing it again.
The problem is your line if not /I "!c1!"== "option1" goto :somewhere_else1
The /I needs to be before not
if /I not "!c1!"== "option1" goto :somewhere_else1
Though I should also mention that your use of ! instead of % is unnecessary in this example.
You should have a read through this link to get some insight into how to fix these errors yourself in the future, and perhaps this link on good practice.
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 /?
Alright, so I am trying to make a batch game. I have already coded/scripted the menu. So I tried to run it to see if it works, but it instantly closes! could anyone take a look at the code/script to help me find out what I am doing wrong?
Also I am using Notepad++
#echo off
title Dungeon Slayer - A Text RPG
:MainMenu
cls
echo.
echo Dungeon Slayer
echo A Text RPG
echo.
echo Build: A1
echo.
echo 1. Play
echo.
echo 2. Settings
echo.
echo 3. Changelog
echo.
echo 4. Exit
echo.
set /p MainMenuSelection
if /p "%MainMenuSelection%" EQU "1" goto :Play
if /p "%MainMenuSelection%" EQU "2" goto :Settings
if /p "%MainMenuSelection%" EQU "3" goto :Changelog
if /p "%MainMenuSelection%" EQU "4" goto :Exit
goto :MainMenu
:Play
cls
echo.
echo Currently Gameplay Development has not been started.
echo.
pause
goto :MainMenu
:Settings
cls
echo.
echo Currently we have not made any settings.
echo.
pause
goto :MainMenu
:Changelog
cls
echo.
echo 11/16/2018
echo.
echo -A1 Build Released
echo.
pause
goto :MainMenu
:Exit
cls
exit
Here's an example using Choice.
#Echo Off
Set "Build=A1"
Set "BDate=11/16/2018"
Set "GName=Dungeon Slayer"
Set "GDesc=A Text RPG"
Title %GName% - %GDesc%
:MainMenu
ClS
Echo=
Echo=%GName%
Echo=%GDesc%
Echo=
Echo Build: %Build%
Echo=
Echo 1. Play
Echo=
Echo 2. Settings
Echo=
Echo 3. Changelog
Echo=
Echo 4. Exit
Echo=
Choice /C 1234 /M "Which item"
If ErrorLevel 4 Exit /B
If ErrorLevel 3 GoTo ChangeLog
If ErrorLevel 2 GoTo Settings
:Play
ClS
Echo=
Echo Sorry, gameplay development has not yet started.
Echo=
Timeout 3 >Nul
GoTo MainMenu
:Settings
ClS
Echo=
Echo Sorry, we are yet to create any settings.
Echo=
Timeout 3 >Nul
GoTo MainMenu
:ChangeLog
ClS
Echo=
Echo=%BDate%
Echo=
Echo -%Build% Build Released
Echo=
Timeout 3 >Nul
GoTo MainMenu
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 =
i have a question . It is possible to make a batch menu to accept multiple commands at the same time?
Example : ( my code )
#ECHO OFF
set tries=6
:top
cls
set /a tries=%tries% -1
if %tries%==0 (
goto penalty
)
Echo You have %tries% attempts left.
Echo Please enter your password to proceed
set /p password=
if %password%==Parola ta (
echo Welcome Your Name
ping localhost -n 5 >nul
cls
Echo CONNECTED!
C:
CD\
CLS
:MENU
CLS
ECHO ============= MENU NAME =============
ECHO -------------------------------------
ECHO 1. System Information TXT file
ECHO 2. Selection 2
ECHO 3. Selection 3
ECHO 4. Selection 4
ECHO 5. Selection 5
ECHO 6. Selection 6
ECHO 7. Selection 7
ECHO -------------------------------------
ECHO 8. Selection 8
ECHO -------------------------------------
ECHO 9. Selection 9
ECHO -------------------------------------
ECHO ==========PRESS 'Q' TO QUIT==========
ECHO.
SET INPUT=
SET /P INPUT=Please select a number:
IF /I '%INPUT%'=='1' GOTO Selection1
IF /I '%INPUT%'=='2' GOTO Selection2
IF /I '%INPUT%'=='3' GOTO Selection3
IF /I '%INPUT%'=='4' GOTO Selection4
IF /I '%INPUT%'=='5' GOTO Selection5
IF /I '%INPUT%'=='6' GOTO Selection6
IF /I '%INPUT%'=='7' GOTO Selection7
IF /I '%INPUT%'=='8' GOTO Selection8
IF /I '%INPUT%'=='9' GOTO Selection9
IF /I '%INPUT%'=='Q' GOTO Quit
CLS
ECHO ============INVALID INPUT============
ECHO -------------------------------------
ECHO Please select a number from the Main
echo Menu [1-9] or select 'Q' to quit.
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO MENU
:Selection1
systeminfo.exe>systeminfo.txt
ECHO ============INVALID INPUT============
ECHO -------------------------------------
ECHO Please select a number from the Main
echo Menu [1-9] or select 'Q' to quit.
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO MENU
:Selection2
Call cleanup.bat
:Selection3
and in here too...
:Selection4
and so on
:Selection5
and so on
:Selection6
and so on
:Selection7
and so on
:Selection8
and so on
:Selection9
and so on
:Quit
CLS
ECHO ==============THANKYOU===============
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE>NUL
EXIT
pause
cls
) else (
goto top
)
goto top
How to make the program accept more than 1 command?
For example 1,3,5 to execute in the same time?
or another question, how its possible to undo .exe back to .bat?
there such a program?
Suggestion: make call from goto and concatenate the commands with &:
IF /I '%INPUT%'=='1' GOTO:Selection1
IF /I '%INPUT%'=='2' CALL:Selection2&CALL:Selection2&CALL:Selection4
IF /I '%INPUT%'=='3' CALL:Selection3&CALL:Selection3
IF /I '%INPUT%'=='4' CALL:Selection4&CALL:Selection4
IF /I '%INPUT%'=='5' CALL:Selection5
IF /I '%INPUT%'=='6' CALL:Selection6
IF /I '%INPUT%'=='7' CALL:Selection7
IF /I '%INPUT%'=='8' CALL:Selection8
IF /I '%INPUT%'=='9' CALL:Selection9
IF /I '%INPUT%'=='Q' GOTO:Quit
To get this work you must alse add goto:eof (eof=end of file) after the jump labels, eg.:
:Selection2
Call cleanup.bat
goto:eof
:Selection3
and in here too...
goto:eof
:Selection4
and so on
goto:eof
:Selection5
and so on
goto:eof
...
..
.
goto:eof returns control to the main program.
Is this what you were looking for?
File Menu.cmd
#echo off
setlocal
set quit=false
set /p InputChoices=Enter Choice(s) (A,B,C)
echo %InputChoices%
call :executeChoices %InputChoices%
endlocal
goto :eof
:executeChoices
if [%1]==[] goto :eof
call :Step%1
shift
goto :executeChoices
goto :eof
:StepA
echo Step A
goto :eof
:StepB
echo Step B
goto :eof
:StepC
echo Step C
goto :eof
Works like this:
c:\>Menu.cmd
Enter Choice(s) (A,B,C) b c a
b c a
Step B
Step C
Step A
You can use the start "" command keyword and each command will run in it's own window, but initiated by your menu batch file.