Nested IF Statements in MS-DOS 6.22 Batch Files
MS-DOS 6.22 does not feature modern block-structured programming or
compound logical operators like AND or OR.
Instead, the command interpreter, COMMAND.COM, processes
nested conditional logic either by sequentially evaluating chained
IF commands placed on a single physical line or by relying
on label jumps using GOTO statements. Understanding this
behavior requires examining how COMMAND.COM parses strings,
evaluates error levels, and reads batch file lines one at a time.
Single-Line Evaluation (Logical AND)
The primary method for executing nested conditional logic directly in
MS-DOS 6.22 is placing multiple IF statements on the same
line:
IF "%1"=="A" IF "%2"=="B" ECHO Both conditions are met.When COMMAND.COM reads this line, it processes the
conditions sequentially from left to right:
- First Evaluation: The interpreter evaluates
"%1"=="A". If this condition is false, execution for that entire line immediately ceases, andCOMMAND.COMmoves to the next line in the batch file. - Subsequent Evaluation: If the first condition
evaluates to true,
COMMAND.COMdiscards the first condition and treats the remainder of the line as an independent command. - Execution: The remaining string begins with
IF "%2"=="B". If this second condition evaluates to true, the trailing command (ECHO Both conditions are met.) is executed.
This behavior acts as a logical AND. However, MS-DOS
6.22 does not support nested block syntax with parentheses (such as
IF condition ( ... )), which was only introduced later in
Windows NT's cmd.exe. Attempting to use parentheses for
multi-line blocks in DOS 6.22 will result in a syntax error or
unexpected execution.
Multi-Line Nested Logic
Using GOTO
Because COMMAND.COM lacks an ELSE statement
and does not support code blocks, complex nested structures must be
constructed by inverting conditions and directing execution flow with
GOTO labels.
@ECHO OFF
IF NOT "%1"=="A" GOTO End
IF NOT "%2"=="B" GOTO End
IF NOT "%3"=="C" GOTO End
ECHO All three conditions passed.
:EndIn this model, COMMAND.COM sequentially processes the
file line-by-line:
- If any condition fails, the interpreter executes the
GOTO Endcommand. - Upon encountering a
GOTO,COMMAND.COMresets its internal file pointer to the beginning of the batch file and performs a linear forward search until it locates the matching label (:End). - Processing then resumes from the line following the label.
Nested ERRORLEVEL
Handling
The IF ERRORLEVEL n statement in MS-DOS 6.22 tests
whether the previous command exited with an exit code greater than or
equal to n. Nesting ERRORLEVEL checks requires
careful ordering, especially when chained with string comparisons:
IF "%DEBUG%"=="TRUE" IF ERRORLEVEL 1 ECHO An error occurred in debug mode.If the string check passes, COMMAND.COM proceeds to
evaluate the exit code. If checking for specific numeric values rather
than ranges, inverted jumps remain the standard practice:
IF ERRORLEVEL 2 GOTO CodeTwoOrHigher
IF ERRORLEVEL 1 GOTO CodeOne
GOTO Success
:CodeTwoOrHigher
ECHO Code is 2 or higher
GOTO End
:CodeOne
ECHO Code is exactly 1
GOTO End
:Success
ECHO Code is 0
:EndVariable Expansion Behavior
In MS-DOS 6.22, variable expansion occurs strictly at the moment a
line is read from the disk. When using nested IF statements
on a single line, all %VARIABLE% references on that line
are expanded simultaneously before any conditions are tested. As a
result, changes made to a variable within a nested statement cannot be
evaluated within the same line, requiring subsequent operations to be
placed on separate, subsequent batch lines.