\
Skip Navigation LinksHome > Coding > Scripting > MS-DOS > ERRORLEVEL Info\
 
 
[]UDF / UDC Library
[]Script Library



 

ERRORLEVEL is the exit code of the last executable program to be invoked, whether from the batch file, or before the batch file was invoked. The ERRORLEVEL remains set to whatever it is until another executable exits. DOS clears this to zero if the executable does not return any exit code. The only thing you can do with ERRORLEVEL is to compare it with a number, and then only with an "equals or is greater than" (if errorlevel => x ) test, because the IF statement will return true for any value equal to or greater than the test value. There are three ways to test for it:

Testing Equal or Greater than
(which is written in Inverse Order):

if errorlevel 255 goto x255
if errorlevel 245 goto x254
. . .
if errorlevel 2 goto x2
if errorlevel 1 goto x1

Test for "ERRORLEVEL is less than"
by using NOT:

if not errorlevel 1 goto xok
:xfail
. . .

:xok
Which in essence performs a < test; and

Isolate individual values
or ranges with a double test

if errorlevel 1 if not errorlevel 2 goto x1
if errorlevel 4 if not errorlevel 7 goto x4
if errorlevel 1 goto xelse
:x0
. . .
:x1
. . .
:x4
. . .
:xelse
. . .
etc.

Note that since ERRORLEVEL is of type "byte", its range is 0 through 255. Note also that there is no '=' in the syntax (it would be incorrect if used because the test is not one of equality).

ERRORLEVEL 0 indicates that the program terminated successfully, assuming that the program actually returned an exit code. However, it must be kept in mind that "success" was defined by the author of the executable and it's meaning may not be quite what the programmer of the batch file would expect.

For example:

XCOPY *.ext c:\temp /a

returns 0 if any files matching *.ext exist in the default directory, whether it actually copies any or not (it wouldn't, unless at least one had its archive attribute bit set). In the example case, the user might expect that ERRORLEVEL 0 would indicate that something had been copied and that ERRORLEVEL 1 would indicate some kind of failure to copy files, and a careful reading of the documentation for XCOPY implies that this is the case - nevertheless, reality is that there is no ERRORLEVEL that indicates that some files matching the pattern were found but none were copied because none satisfied the auxiliary test (the /a switch). DOS is like that - the wise batch file programmer is careful to test each element of the program's action separately.

Help Support Dx21
Buy Thru Us

 FAQs  |  Terms Of Use  |  Privacy Policy  |  Contact Us
Copyright © 1997 - 2010 Dx21, LLC. All rights reserved.
Dx21, LLC a Washington Limited Liability Company
Page Rendered at: 9/2/2010 8:16:18 PM for Unknown