Testing for a Specific Error Level in Batch Files

This article was previously published under Q69576
This article has been archived. It is offered "as is" and will no longer be updated.
SUMMARY
The Microsoft MS-DOS "IF ERRORLEVEL <number>" statement checks forerror levels of the given number or greater. If you want to check fora specific error level, you should use the following command construct
   IF ERRORLEVEL <N> IF NOT ERRORLEVEL <N+1> <COMMAND>				
where <N> is the desired number. The <N+1> portion of the command mustbe calculated as the command is entered, because the MS-DOS commandinterpreter cannot perform mathematical calculations.
MORE INFORMATION
The nested IF statements allow specific selection of an error levelbecause the NOT operator effectively reverses the inequality. Thecommand
   IF ERRORLEVEL 5 ...				
is equivalent to the algebraic construct
   IF E = 5 OR E > 5 THEN ...				
while the command
   IF NOT ERRORLEVEL 6				
is equivalent to the algebraic construct:
   IF E < 6 THEN ...				
The combination of the two IF commands works as described because thesecond IF is executed only if the first is true, and the <command> isexecuted only if the second is true; therefore, the combination of the twocommands is executed only if BOTH are true. Because the desired error levelis bracketed with the two tests, the entire conditional is TRUE only whenERRORLEVEL is exactly that value.

This same syntax can be expanded to cover a sequential range of ERRORLEVELreturn codes by increasing the difference between the values checked for.
6.22 3.20 3.21 3.30 3.30a 4.00 4.01 5.00 6.00 6.20
Properties

Article ID: 69576 - Last Review: 12/04/2015 09:06:38 - Revision: 2.1

Microsoft MS-DOS 3.1, Microsoft MS-DOS 3.2 Standard Edition, Microsoft MS-DOS 3.21 Standard Edition, Microsoft MS-DOS 3.3 Standard Edition, Microsoft MS-DOS 3.3a, Microsoft MS-DOS 4.0 Standard Edition, Microsoft MS-DOS 4.01 Standard Edition, Microsoft MS-DOS 5.0 Standard Edition, Microsoft MS-DOS 5.0a, Microsoft MS-DOS 6.0 Standard Edition, Microsoft MS-DOS 6.2 Standard Edition, Microsoft MS-DOS 6.21 Standard Edition, Microsoft MS-DOS 6.22 Standard Edition

  • kbnosurvey kbarchive KB69576
Feedback
" statement checks for error levels of the given number or greater. If you want to check for a specific error level, you should use the following command construct IF ERRORLEVEL IF NOT ERRORLEVEL ..." />
" statement checks for error levels of the given number or greater. If you want to check for a specific error level, you should use the following command construct IF ERRORLEVEL IF NOT ERRORLEVEL ..." />