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 =
Related
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
This code is supposed to make it so that if I press 1 - 4 it would go the the options 1 - 4 and if i press something else it would say that its not an option. But if I type something else it goes to debug anyway.
And I've checked the code several times and I can't find an error. I even compared it to a code like this that actually worked and I even Ctrl-c Ctrl-v'ed it and it still won't work.
:admin
cls
color %debug%
echo You have accessed the admin debug menu
echo Do what you wan't
echo [1] Set Debug color
echo [2] Set Default color
echo [3] Set Warning color
echo [4] Exit
set /p "lol=> "
IF /i %lol%==1 goto dbg if NOT goto 9
IF /i %lol%==2 goto dfc if NOT goto 9
IF /i %lol%==3 goto wnc if NOT goto 9
IF /i %lol%==4 goto start if NOT goto 9
:dbg
cls
color %debug%
echo Set Debug color
set /p "debug=> "
echo Debug color set to %debug%
color %debug%
pause
goto admin
:dfc
cls
color %debug%
echo Set Default color
set /p "default=> "
echo Default color set to %default%
pause
goto admin
:wnc
cls
color %debug%
echo Set Warning color
set /p "warning=> "
echo Warning color set to %warning%
pause
goto admin
:9
cls
color %warning%
echo This is not a viable option!
ping localhost -n 5 >nul
goto admin
I test it out and when i type ex 5 it still goes to debug.
You should take a closer look at how IF statements work.
E.g. this line IF /i %lol%==1 goto dbg if NOT goto 9 makes absolutely no sense.
Your code should look something like this:
...
set /p "lol=> "
IF /i "%lol%"=="1" goto dbg
IF /i "%lol%"=="2" goto dfc
IF /i "%lol%"=="3" goto wnc
IF /i "%lol%"=="4" goto start
cls
color %warning%
echo This is not a viable option!
ping localhost -n 5 >nul
goto admin
...
IF /i %lol%==4 (goto start) else goto 9
Although it's normal practise to use
IF /i "%lol%"=="4" (goto start) else goto 9
which provides some protection against no entry or an entry containing spaces.
try this code
IF /i %lol%==1
(goto dbg)
IF /i %lol%==2
(goto dfc)
IF /i %lol%==3
(goto wnc)
IF /i %lol%==4
(goto start)
else
(goto 9)
I am building a simple calculator, and am wondering why my if statement is not working. Can anyone help?
Code:
title Addition
color a
#echo off
cls
:add
cls
echo What would you like to do?
echo [1]Set first number [2]Set second number [3]Add 1 and 2 [4]Quit [5]Main Menu
set /p act=
if act==1 then goto :1
if act==2 then goto :2
if act==3 then goto :num
if act==4 then goto :quit
if act==5 then goto :menu
:1
cls
echo What is your first number?
set /p 1=
cls
goto :add
:2
cls
echo What is your second number?
set /p 2=
cls
goto :add
:num
num=1+2
echo Your number is %num%
pause
cls
goto :add
:quit
:menu
SimpleCalulator.bat
To get the value of a variable, you need to surround it with % (or ! when using delayed expansion). You also need to quote your variables unless you really have a good reason not to. Also, "then" is not a batch-file keyword.
if "%act%"=="1" goto :1
if "%act%"=="2" goto :2
if "%act%"=="3" goto :num
if "%act%"=="4" goto :quit
if "%act%"=="5" goto :menu
REM Don't forget to handle unexpected user input:
goto :add
I was creating a simple random number guesser in batch but I got an error in my code and don't know what it is please help. Here is the code:
#echo off
title Number Guesser
:menu
echo ------------------
echo Number Guesser
echo ------------------
echo 1. Easy
echo 2. Medium
echo 3. Hard
echo 4. Exit
set /p dif=Select difficulty number:
if %dif% == 1 goto easygen
if %dif% == 2 goto medgen
if %dif% == 3 goto hardgen
if %dif% == 4 exit
goto menu
:easygen
set /a num=%random%
if %num% gtr 20 goto gen
cls
goto play
:medgen
set /a num=%random%
if %num% gtr 50 goto gen
cls
goto play
:hardgen
set /a num=%random%
if %num% gtr 100 goto gen
cls
goto play
:play
set /p guess=Guess:
if %guess% == %num% goto win
if %guess% gtr %num% echo Lower!
if %guess% lss %num% echo Higher!
:win
cls
echo Well Done
echo 1. Play again!
echo 2. Quit
set /p cmd=What do you want to do:
if %cmd% == 1 goto menu
if %cmd% == 2 exit
I get the error once I choose the difficulty (dif) I have no idea what isn't working. It just closes.
If you open up cmd then navigate to the file and run it, instead of double clicking the batch file, you'll see an error.
The system cannot find the batch label specified - gen
Like the other commenter said, you don't have :gen defined. It says goto gen, but gen doesn't exist, so it freaks out and closes.
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.