What are error levels?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

What are error levels?

#1 Post by MLGsuperGame414 » 12 Nov 2011 19:44

Sorry guys but I keep seeing error level posts on the forums and I am extremely new to batch coding. Can someone please explain to me what it is. I have a hint of what it might be but I'm not sure how it works.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: What are error levels?

#2 Post by Ed Dyreen » 13 Nov 2011 00:04

'
Well, it's what makes us detect when things go wrong, or right:

Code: Select all

set /a $? = 0
echo.errorlevel=%errorlevel%_
set /a $? = 9999999999
if %errorlevel% neq 0 (
   echo.something went wrong...
)

pause
exit
Checking and reacting to errorlevels is one of the keys to programming.

Sometimes we want to set the errorlevel ourselves, it is good style to NEVER assign a value to errorlevel directly, like:

Code: Select all

set /a errorlevel = 1
but why ? :)

aGerman
Expert
Posts: 4735
Joined: 22 Jan 2010 18:01
Location: Germany

Re: What are error levels?

#3 Post by aGerman » 13 Nov 2011 07:00

The errorlevel is the returned value of a called command, program or script. It depends on the developer whether you will get back a nonzero value if an error occurs or which value is for which program state.

For example find.exe will return errorlevel 0 if the search string is found and 1 if it's not found.

Code: Select all

@echo off
echo Hello|find "e"
echo ERRORLEVEL: %errorlevel%
echo(
echo Hello|find "a"
echo ERRORLEVEL: %errorlevel%
echo(
pause

Regards
aGerman

MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

Re: What are error levels?

#4 Post by MLGsuperGame414 » 13 Nov 2011 07:05

Okay Okay, I think I understand it. Thanks guys.

aGerman
Expert
Posts: 4735
Joined: 22 Jan 2010 18:01
Location: Germany

Re: What are error levels?

#5 Post by aGerman » 13 Nov 2011 07:37

I forgot to mention...
Ed Dyreen wrote:Sometimes we want to set the errorlevel ourselves, it is good style to NEVER assign a value to errorlevel directly, like:

Code: Select all

set /a errorlevel = 1
but why ? :)

Have a look at the IF - help.
%ERRORLEVEL% will expand into a string representation of
the current value of ERRORLEVEL, provided that there is not already
an environment variable with the name ERRORLEVEL, in which case you
will get its value instead
.


Code: Select all

@echo off
echo Hello|find "a"
echo ERRORLEVEL: %errorlevel%
echo(

:: never do that
set /a errorlevel=0

echo Hello|find "a"
echo ERRORLEVEL: %errorlevel%
echo(
pause


Regards
aGerman

MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

Re: What are error levels?

#6 Post by MLGsuperGame414 » 14 Nov 2011 16:05

Okay so would this work?

Set %errorlevel%
Echo.
@echo off
Echo 1). There is no other option choose this one.
set /p cs=Select your option;
If "%cs%"=="1" winver else %errorlevel%=Invalid Choice please try again.
Echo %errorlevel%
Echo.

Post Reply