How IF ERRORLEVEL Works in MS-DOS 6.22 Batch Files

In MS-DOS 6.22, batch scripts utilize the IF ERRORLEVEL command to evaluate exit codes returned by executed programs and utilities. This mechanism enables conditional branching, allowing a script to respond dynamically to successful operations, non-fatal warnings, or critical system errors. Understanding how the MS-DOS command processor (COMMAND.COM) evaluates these numeric codes is essential for writing reliable automation routines.

The Return Code Mechanism

When an executable program or MS-DOS command finishes running, it terminates by calling an interrupt—typically INT 21h, Function 4Ch—which passes an 8-bit exit code (ranging from 0 to 255) back to the operating system. COMMAND.COM captures this value internally and holds it for evaluation. By convention, a return code of 0 signals successful completion, while values of 1 through 255 represent various warnings or error states determined by the application's developer.

The "Greater Than or Equal To" Rule

The most critical aspect of the IF ERRORLEVEL statement in MS-DOS 6.22 is that it does not test for exact equality. Instead, the condition evaluates as true if the exit code is greater than or equal to the specified number:

IF ERRORLEVEL number command

If an executable returns an exit code of 3, the statement IF ERRORLEVEL 1 evaluates to true because 3 is greater than or equal to 1. Consequently, standard equality checks (such as checking if a code is strictly 1) cannot be performed using a single direct statement.

Sequencing Checks in Descending Order

Because of the "greater than or equal to" evaluation logic, multiple IF ERRORLEVEL checks must always be arranged in descending numerical order (from highest value to lowest value).

If statements are listed in ascending order, the lowest condition will trigger prematurely:

:: INCORRECT SEQUENCING
XCOPY C:\DATA D:\BACKUP
IF ERRORLEVEL 1 GOTO WARNING
IF ERRORLEVEL 4 GOTO CRITICAL_ERROR

In the incorrect example above, if XCOPY returns 4 (an initialization or disk error), the script encounters IF ERRORLEVEL 1 first. Because 4 is greater than or equal to 1, the script jumps to :WARNING, and the :CRITICAL_ERROR check is never reached.

To handle errors correctly, structure the script from highest to lowest:

:: CORRECT SEQUENCING
XCOPY C:\DATA D:\BACKUP
IF ERRORLEVEL 5 GOTO DISK_WRITE_ERROR
IF ERRORLEVEL 4 GOTO INITIALIZATION_ERROR
IF ERRORLEVEL 2 GOTO TERMINATED_BY_USER
IF ERRORLEVEL 1 GOTO NO_FILES_FOUND
IF ERRORLEVEL 0 GOTO SUCCESS

In this sequence, the script tests for the highest code first. If the return code is 4, the check for 5 evaluates to false, the check for 4 evaluates to true, and execution jumps directly to the intended label.

Using the NOT Modifier

MS-DOS 6.22 also supports the NOT operator with ERRORLEVEL:

IF NOT ERRORLEVEL number command

Due to the inverse logic, IF NOT ERRORLEVEL number evaluates to true only if the exit code is strictly less than the specified number. For example:

FORMAT A: /U
IF NOT ERRORLEVEL 1 GOTO ALL_CLEAR

In this case, the script proceeds to :ALL_CLEAR only if the exit code is 0. Any code of 1 or higher causes the condition to fail, allowing subsequent error-handling routines to run.

Limitations in MS-DOS 6.22

Unlike modern Windows command environments, MS-DOS 6.22 does not support ERRORLEVEL as an expanded dynamic environment variable (such as %ERRORLEVEL%). You cannot evaluate exit codes using standard comparison operators (like IF %ERRORLEVEL%==2). The exit code can only be tested using the explicit IF [NOT] ERRORLEVEL syntax provided by COMMAND.COM.