Choice and Errorlevel? - batch-file

I do something like this:
echo 1-exit
echo 2-about
echo 3-play
choice /c 123 >nul
if errorlevel 1 goto exit
if errorlevel 2 goto about
if errorlevel 3 goto play
:play
blah
:about
blah
:exit
cls
If I select the "play" option, it exits. How do I prevent this from happening?

The if errorlevel expression evaluates to true if actual error level returned by choice is greater or equal to given value. So if you hit 3, the first if expression is true and script terminates. Call help if for more information.
There are two simple workarounds.
First one (better) - replace if errorlevel expression with actual comparision of %ERRORLEVEL% system variable with a given value:
if "%ERRORLEVEL%" == "1" goto exit
if "%ERRORLEVEL%" == "2" goto about
if "%ERRORLEVEL%" == "3" goto play
Second one - change order of comparisions:
if errorlevel 3 goto play
if errorlevel 2 goto about
if errorlevel 1 goto exit

The easiest way to solve this problem is to use the %errorlevel% value to directly go to the desired label:
echo 1-exit
echo 2-about
echo 3-play
choice /c 123 >nul
goto option-%errorlevel%
:option-1
rem play
blah
:option-2
rem about
blah
:option-3
exit
cls

Related

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

How to use the choice command?

I need the choice command to do the same thing as this block of code:
set /p start= Select Mode:
if %start% == 1 goto money
if %start% == 2 goto payouts
if %start% == 3 goto tutorial
if %start% == 4 exit /b
I have been trying for quite some time now and cannot figure it out Thanks in advance!
The simplest solution is not check the value of errorlevel at all, but directly use it in a goto command. In this way, you avoid the usual series of if commands, so the code is simpler.
choice /C 1234 /M "Select Mode: "
goto option-%errorlevel%
The rest of the code shoud be like this:
:option-1 money
echo Money
goto loop
:option-2 payouts
echo Payouts
goto loop
:option-3 tutorial
echo Tutorial
goto loop
:option-4
exit /B
Using the command choice like this
choice /c 1234 /M "Select Mode: "
upon exit, errorlevel will be set to the index (starting with 1) of the selected choice. For acting upon the errorlevel it's important to remember that "if errorlevel n" traps not only n, but also all higher values, meaning that they need to be specified in reverse
if errorlevel 4 goto exit
if errorlevel 3 goto tutorials
if errorlevel 2 goto payout
if errorlevel 1 goto money
An alternative to the answer by fvu, is to use the %ErrorLevel% variable as Squashman suggested:
Choice /C 1234 /M "Select Mode: "
If %ErrorLevel%==1 GoTo money
If %ErrorLevel%==2 GoTo payouts
If %ErrorLevel%==3 GoTo tutorial
Exit/B
[:money | :payouts | :tutorial]

Batch ERRORLEVEL not working properly

I've been using batch for a while now and I just recently ran into a problem I never encountered before involving ERRORLEVELS.
Here is a short program I made to show off the error.
#echo off
title Choices
CMD /C EXIT 0
echo [1] Choice 1
echo [2] Choice 2
echo [3] Choice 3
choice /c 123 /n
IF ERRORLEVEL 1 GOTO ONE
IF ERRORLEVEL 2 GOTO TWO
IF ERRORLEVEL 3 GOTO THREE
echo Nice you broke it
pause
exit
:ONE
echo CONGRATS YOU CHOSE 1
pause
exit
:TWO
echo NICE YOU CHOSE 2
pause
exit
:THREE
echo OOH YOU CHOSE 3
pause
exit
Its very simple and all you do is press a number and it says what number you pressed. The problem is no matter what i press it always outputs what would happen when I press 1. I used to use %errorlevel% and that worked fine but then it stopped working so I switched to the new method (IF ERRORLEVEL WHATEVER) and now it wont work either.
Please read the Microsoft support article Testing for a Specific Error Level in Batch Files.
And open a command prompt window, run if /? and read the output help, especially the paragraph about errorlevel.
The solution for your batch file is very simple, reverse the order of the lines testing on errorlevel:
#echo off
title Choices
CMD /C EXIT 0
echo [1] Choice 1
echo [2] Choice 2
echo [3] Choice 3
choice /c 123 /n
IF ERRORLEVEL 3 GOTO THREE
IF ERRORLEVEL 2 GOTO TWO
IF ERRORLEVEL 1 GOTO ONE
echo Nice you broke it
pause
exit
:ONE
echo CONGRATS YOU CHOSE 1
pause
exit
:TWO
echo NICE YOU CHOSE 2
pause
exit
:THREE
echo OOH YOU CHOSE 3
pause
exit

What's Wrong With My Batch File

Trying to create a batch file to make these tasks run faster.
Can someone please correct my errors or suggest a better way to write this script
Basically everytime I run it says "request timed out"
#echo off
color 0a
Title
:Beginning
set /p UserInput = What Would You Like To Start?
echo.
if %UserInput% == 1 goto :Windows Update
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 2 goto :Group Policy Update
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 5 goto :Favorites
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 3 goto :Tools
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 4 goto :Printer
if not %UserInput% == "" goto :Exit
else goto :Exit
:Windows Update
start C:\Windows\system32\wuapp.exe
pause
exit
:Group Policy Update
start gpupdate.exe
pause
exit
:Favorites
move %userprofile%\favorites\*.* G:\
pause
exit
:Tools
start \\NoneOFyourBusiness
pause
exit
:Printer
start iexplore.exe http://www.Google.com
pause
exit
:Exit
set /p beginning == Return To The Start?
echo.
echo Y=Yes or N=No
if %beginning% == "Y" goto :Beginning
if not %beginning% == "N" goto :Exit 2
:Exit 2
pause
exit**'
set /p UserInput = What Would You Like To Start?
Will apply the input string to a variable named UserInputSpace
Batch is sensitive to spaces on both sides of the = in a set (but not a set /a)
if not %UserInput% == "" goto :Exit
Because you have no control over the use's input, that may contain spaces and other character to which batch is sensitive.
use
if not "%UserInput%"=="" goto :Exit
Note that the two strings must be exactly equal. IF /i ... will make the test case-insensitive.
Note also that responding simply Enter to a set /p will leave the variable unchanged, it will not "set" it to an empty string. If you want that, use
set "var="
set /p var=
Also, to detect whether var has been set, use
if defined var
if "%UserInput%"=="1" goto :Windows Update
This is a fail-to-fail scenario. The real command executed will be
if "%UserInput%"=="1" goto Windows
The remaining text after the space-separator is documentation.
Not a good idea IMHO to use the colon in a goto. It works, but there is a difference with a CALL statement. CALL :something will execute an internal subroutine (ie subroutine in this batch file) named something, whereas CALL something without the colon will call an external executable. By analogy, GOTO should also not be used with a colon. The one exception is the documented special condition, GOTO :EOF where the colon is required. GOTO :EOF means 'go to the end of the physical batch file.The labelEOFneed not (indeed **should** not) be included in the batch.CMD` knows where it has to go...
So - fix those and it should work a lot better. Not sure what the error response you have is, but I'd suggest it's some utility you'd not expected to run getting tired of waiting for input.
Try this...
color 0a
Title Testing
REM Since exit is a keyword - changed to leave
:Begining
echo choose an option
choice /c 12345
rem set /p UserInput = What Would You Like To Start?
echo.
if %errorlevel% == 1 goto WindowsUpdate
if %errorlevel% == 2 goto GroupPolicyUpdate
if %errorlevel% EQU 5 goto Favorites
if %errorlevel% EQU 3 goto Tools
if %errorlevel% EQU 4 goto Printer
:WindowsUpdate
start C:\Windows\system32\wuapp.exe
pause
goto leave
:GroupPolicyUpdate
start gpupdate.exe
pause
goto leave
:Favorites
move %userprofile%\favorites\*.* G:\
pause
goto leave
:Tools
start \\NoneOFyourBusiness
pause
goto leave
:Printer
start iexplore.exe http://www.Google.com
pause
goto leave
:leave
echo leave the script?
choice /c YN
if %errorlevel% NEQ 1 goto Begining
if %errorlevel% EQU 1 exit
** #echo off
color 0a
Title
:Beginning
set UserInput=Error
set /p UserInput=What Would You Like To Start?
echo.
if "%UserInput%"=="WU" goto Windows Update
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="GPU" goto Group Policy Update
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="Fav" goto Favorites
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="T" goto Tools
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="P" goto Printer
if not "%UserInput%"=="" goto Exit
:Windows Update
start C:\Windows\system32\wuapp.exe
goto exit
:Group Policy Update
start gpupdate.exe
goto exit
:Favorites
move %userprofile%\favorites\*.* G:\
goto exit
:Tools
start \\T
goto exit
:Printer
start iexplore.exe http://Google.com/
:Exit
set Beginning==Error
set /p Beginning==Return To The Start?
echo.
echo y=Yes or n=No
if "%Beginning%"=="y" call :Beginning
if not "%Beginning%"=="n" goto Exit 2
:Exit 2
pause
exit**

Using CHOICE with CMD script

Having trouble getting this CHOICE script to work. Can anyone provide any insight?
#echo off
CHOICE /C:IRCQSH /T 10 /N /M "Waiting with choice..." /D H
IF ERRORLEVEL 0 ECHO "Default choice: Health"
IF ERRORLEVEL 1 ECHO "Install"
IF ERRORLEVEL 2 ECHO "Remove"
IF ERRORLEVEL 3 ECHO "Console"
IF ERRORLEVEL 4 ECHO "Quit"
IF ERRORLEVEL 5 ECHO "Start"
IF ERRORLEVEL 6 ECHO "Health"
pause
You need to change your syntax to treat ERRORLEVEL as a variable, and use the CMD equality statements, such as:
IF %ERRORLEVEL% EQU 0 ECHO "Default choice: Health"
IF %ERRORLEVEL% EQU 1 ECHO "Install"
IF %ERRORLEVEL% EQU 2 ECHO "Remove"
IF %ERRORLEVEL% EQU 3 ECHO "Console"
IF %ERRORLEVEL% EQU 4 ECHO "Quit"
IF %ERRORLEVEL% EQU 5 ECHO "Start"
IF %ERRORLEVEL% EQU 6 ECHO "Health"
The reason your code is failing is, taken from here:
IF ERRORLEVEL n statements should be read as IF Errorlevel >= number
i.e.
IF ERRORLEVEL 0 will return TRUE when the errorlevel is 64
A couple points here:
The default choice does NOT return an ERRORLEVEL of zero, but the number of the choice selected. In your case, that is H, the default is equal to press H with an ERRORLEVEL of 6
The right way to take the value of ERRORLEVEL is enclosing it in percents and use the EQU comparison, as LittleBobbyTables said in his answer. However, there are other ways to achieve the same result.
The IF ERRORLEVEL Number Command test if the errorlevel value is Greater or Equal than the given number, so you may also use this form:
.
#echo off
CHOICE /C:IRCQSH /T 10 /N /M "Waiting with choice..." /D H
FOR %%E IN (6 5 4 3 2 1) DO IF ERRORLEVEL %%E GOTO LABEL-%%E
:LABEL-1
ECHO "Install"
GOTO CONTINUE
:LABEL-2
ECHO "Remove"
GOTO CONTINUE
:LABEL-3
ECHO "Console"
GOTO CONTINUE
:LABEL-4
ECHO "Quit"
GOTO CONTINUE
:LABEL-5
ECHO "Start"
GOTO CONTINUE
:LABEL-6
ECHO "Health"
:CONTINUE
pause
Perhaps the simplest way to achieve the same thing is defining an array and show the appropriate element using the errorlevel value as index:
.
#echo off
setlocal EnableDelayedExpansion
rem Create an array with the desired messages (selected by numeric index)
set index=0
for %%a in ("Install" "Remove" "Console" "Quit" "Start" "Health") do (
set /A index+=1
set elem[!index!]=%%a
)
CHOICE /C:IRCQSH /T 10 /N /M "Waiting with choice..." /D H
echo !elem[%ERRORLEVEL%]!
pause
For a further description of Batch arrays, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

Resources