Batch ERRORLEVEL not working properly - batch-file

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

Related

All option in choice command

I am wondering if there is a nice and clean way to add a "select all choices" when building a choice menu in batch.
Currently I have the choice setup as clear item 1, clear item 2, clear item 3 (clear all), exit, and a timeout that the user cannot see.
If I have to I will just re-add all my code to the "clear all" area. I was hoping to see if there was a way to just have the "clear all" use the defined 1 and 2 and then go back to :start like everything else.
Answer: I took Aacini's idea of the "set option" and came up with an even simpler answer. I Changed "GOTO :start"to"GOTO :ClearCache" for the "Clear All Options". Then I added a "IF %ERRORLEVEL% neq 3 GOTO :start" and after that a "GOTO :ClearCredentials". This allowed me to keep less lines than the set option and I didn't have to move my code around to have it pass to the next process.
This should allow for multiple but different clear all options for future items.
#ECHO OFF
:start
ECHO 1. Clear IE, Chrome, Temp Cache
ECHO 2. Clear Credentials in IE, Chrome, and Windows
ECHO 3. Clear All Options
ECHO 4. Exit
CHOICE /N /C:12345 /T 15 /D 5 /M "Type the number to choose an option."
IF %ERRORLEVEL%==5 GOTO TIMEOUT
IF %ERRORLEVEL%==4 GOTO Exit
IF %ERRORLEVEL%==3 GOTO ClearAllOptions
IF %ERRORLEVEL%==2 GOTO ClearCredentials
IF %ERRORLEVEL%==1 GOTO ClearCache
GOTO :start
:ClearCache
ECHO Clearing Cache...
<code here>
pause
cls
IF %ERRORLEVEL% neq 3 GOTO :start
GOTO :ClearCredentials
REM ===-----------------------
:ClearCredentials
ECHO Clearing Credentials
<code here>
pause
cls
GOTO :start
REM ===-----------------------
:ClearAllOptions
ECHO Clearing All Options...
pause
cls
GOTO :ClearCache
pause
REM ===-----------------------
:Exit
ECHO Exiting...
<code here>
pause
EXIT
REM ===-----------------------
:TIMEOUT
cls
ECHO Exiting because no choices were made in 15 seconds
:END
timeout /t 5
This is a very simple way to do that:
#ECHO OFF
:start
set option=0
ECHO 1. Clear IE, Chrome, Temp Cache
ECHO 2. Clear Credentials in IE, Chrome, and Windows
ECHO 3. Clear All Options
ECHO 4. Exit
CHOICE /N /C:12345 /T 15 /D 5 /M "Type the number to choose an option."
GOTO Option-%ERRORLEVEL%
:Option-3 ClearAllOptions
ECHO Clearing All Options
set option=3
:Option-1 ClearCache
ECHO Clearing Cache...
<code here>
pause
cls
if %option% neq 3 GOTO :start
REM ===-----------------------
:Option-2 ClearCredentials
ECHO Clearing Credentials
<code here>
pause
cls
GOTO :start
REM ===-----------------------
:Option-4 Exit
ECHO Exiting...
<code here>
pause
EXIT
REM ===-----------------------
:Option-5 TIMEOUT
cls
ECHO Exiting because no choices were made in 15 seconds
:END
timeout /t 5

How to read text from command prompt using a .bat

I am trying to get into windows batch files and programming in general, I messed around with an example I found online and made a batch file that creates another batch file (for the sake of learning)
But I now want to enable typing into a command prompt, I have a crude solution to this where I make a batch file, then create another batch file and start that file using the orignal .bat (don't worry, code is below, will make sense.)
Basically if you type 1, I want to do something in the batch, I know that I need to first do an if, but it's trying to get the text entered in the command line that I'm struggling with.... Any help would be great!
#echo off
if exist CommandTest.bat (
echo File already exists...
pause
) else (
echo CommandTest.bat does not exist. Creating the file...
timeout /t 3 /NOBREAK>nul
echo Please wait while your file loads...
echo #echo off >CommandTest.bat
echo color 0a >>CommandTest.bat
echo echo Line number 1 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
echo echo Line number 2 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
echo echo Line number 3 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
echo echo Line number 4 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
timeout /t 2 /NOBREAK>nul
start CommandTest.bat
timeout /t 5 /NOBREAK>nul
)
I am basically trying to redesign it to be user interactive...
According to your comment, choice is the right choice for you:
#echo off
echo 1 - do something
echo 2 - do something else
echo 3 - do nothing
choice /c 123 /m "take your choice "
if %errorlevel% == 1 echo let's do something
if %errorlevel% == 2 goto :other
if %errorlevel% == 3 echo let's do nothing
pause
goto :eof
:other
echo let's do something else
echo something else ...
pause

Windows Embeded Compact 7 - using batch file - ERRORLEVEL not possible?

I tried to write an batch file using ERRORLEVEL.
Unfortunatly it´s not working (or better not working the way I expect).
It seems to be that ERRORLEVEL is not available as an environment variable.
on cmd:
if %errorlevel% == 0 echo test
result:
no echo
on cmd:
if %errorlevel% == %errorlevel% echo test
result:
test
on cmd:
echo %errorlevel%
result:
%errorlevel%
Read about using setlocal, but setlocal cannot be executed
Any ideas?
Thank you in advance!
ERRORLEVEL requires a process or function to be carried out in order to display an input. Say for instance you wanted to Ping Google and if successful it would print out "Good" or if it failed printed out "Bad", that's where ERRORLEVEL would come into effect. You cannot use ERRORLEVEL if a process or function is not present.
EX.
echo off
goto :pingtest
cls
:pingtest
cls
ping www.google.com
if errorlevel 2 goto :good
if errorlevel 1 echo :bad
:good
cls
echo Good
echo[
pause
:bad
cls
echo Bad
echo[
pause
As you can see from the code above, there was a process involved.
Also if you get the chance could you check out my blog? http://pryrotech.weebly.com

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

Choice and Errorlevel?

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

Resources