Testing ERRORLEVEL in MS-DOS 6.22 Utilities

This article explains how MS-DOS 6.22 monitors command execution and identifies the specific parameter tested to determine whether a utility succeeded or failed. Readers will learn about the ERRORLEVEL exit code mechanism, how it is evaluated using standard batch commands, and the best practices for structuring conditional error checks in classic DOS environments.

In MS-DOS 6.22, the parameter tested after running an executable utility or command is ERRORLEVEL. When a DOS utility finishes running, it returns an integer exit code (ranging from 0 to 255) to the operating system. An exit code of 0 traditionally indicates successful completion without errors, whereas values of 1 or greater indicate warnings, user cancellations, or fatal errors.

The IF ERRORLEVEL Syntax

MS-DOS does not automatically display the return code on screen. Instead, batch files test the parameter using the conditional IF ERRORLEVEL statement:

IF ERRORLEVEL number command

A critical behavior of MS-DOS 6.22 is that the condition evaluates to true if the return code is greater than or equal to the specified number. Because of this greater-than-or-equal logic, batch scripts must test error levels in descending order (highest numerical value to lowest) to prevent lower threshold checks from preempting more severe error codes.

Common Utility Return Codes

Different built-in utilities in MS-DOS 6.22 implement their own standardized ERRORLEVEL values:

  • XCOPY:

    • 0: Files were copied without error.
    • 1: No files were found to copy.
    • 2: The user pressed CTRL+C to terminate the operation.
    • 4: Initialization error (insufficient memory, invalid drive, or bad syntax).
    • 5: Disk write error.
  • FORMAT:

    • 0: Format operation completed successfully.
    • 3: The user aborted the process with CTRL+C.
    • 4: Fatal processing error occurred.
    • 5: The user answered "No" to the prompt to format a fixed disk.
  • CHOICE:

    • Returns an integer corresponding to the position of the key pressed in the option list (for example, the first option returns 1, the second returns 2), while 0 indicates that the user pressed CTRL+C.

Practical Implementation Example

To correctly handle errors from a command like XCOPY, a batch script checks the highest error levels first:

@ECHO OFF
XCOPY C:\DATA\*.* D:\BACKUP\
IF ERRORLEVEL 5 GOTO DiskError
IF ERRORLEVEL 4 GOTO InitError
IF ERRORLEVEL 2 GOTO Aborted
IF ERRORLEVEL 1 GOTO NoFiles
IF ERRORLEVEL 0 GOTO Success

:DiskError
ECHO A disk write error occurred.
GOTO End

:InitError
ECHO Initialization or syntax error.
GOTO End

:Aborted
ECHO Operation was terminated by the user.
GOTO End

:NoFiles
ECHO No files were found to copy.
GOTO End

:Success
ECHO Backup completed successfully.

:End

By querying ERRORLEVEL immediately after a utility executes, MS-DOS 6.22 scripts maintain flow control, protect system integrity, and offer automated recovery steps during runtime operations.