What are error levels?
Moderator: DosItHelp
-
- Posts: 54
- Joined: 10 Nov 2011 20:40
What are error levels?
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.
Re: What are error levels?
'
Well, it's what makes us detect when things go wrong, or right:
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:
but why ? 
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
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

Re: What are error levels?
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.
Regards
aGerman
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
-
- Posts: 54
- Joined: 10 Nov 2011 20:40
Re: What are error levels?
Okay Okay, I think I understand it. Thanks guys.
Re: What are error levels?
I forgot to mention...
Have a look at the IF - help.
Regards
aGerman
Ed Dyreen wrote:Sometimes we want to set the errorlevel ourselves, it is good style to NEVER assign a value to errorlevel directly, like:but why ?Code: Select all
set /a errorlevel = 1
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
-
- Posts: 54
- Joined: 10 Nov 2011 20:40
Re: What are error levels?
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.
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.