Using GOTO in MS-DOS 6.22 Batch Files
This article provides an overview of the GOTO statement
in MS-DOS 6.22 batch file programming, explaining its purpose, syntax,
and operational mechanics. It details how the command directs script
execution flow, how labels are defined and processed by the command
interpreter, how GOTO is integrated with conditional
statements to create branches and loops, and the technical limitations
unique to the MS-DOS 6.22 environment.
Syntax and Label Definition
In MS-DOS 6.22, the GOTO command transfers control of
the script execution to a specified line marked by a label. The syntax
requires two elements: the jump command and the destination label.
GOTO label_name
...
:label_nameA label is identified by a line beginning with a colon
(:) followed immediately by an identifier. When
COMMAND.COM encounters the GOTO label_name
directive, it halts sequential processing and searches the file for a
matching :label_name, resuming execution on the line
immediately following that label.
In MS-DOS 6.22, labels are not case-sensitive, meaning
:START, :Start, and :start are
treated identically. Furthermore, the interpreter only evaluates the
first eight characters of a label. Any characters beyond the eighth are
ignored, meaning labels like :PROCESS_FILE_A and
:PROCESS_FILE_B will be seen as identical
(:PROCESS_), causing unexpected jumps.
How the Interpreter Processes GOTO
When a GOTO command is executed,
COMMAND.COM closes its current file pointer position and
typically scans the batch file from the very beginning to locate the
target label. Because of this rewinding mechanism:
- Labels placed higher up or lower down in the script can both be targeted (allowing forward jumps and backward jumps).
- In exceptionally large batch files running on vintage hardware, frequent jumps can incur noticeable disk or memory read overhead.
- If the interpreter reaches the end of the file without finding the
specified label, it terminates the script and outputs the error message:
Label not found.
Conditional Branching
The primary utility of GOTO in MS-DOS 6.22 is executing
conditional branches in combination with the IF statement.
Because MS-DOS 6.22 lacks block-level conditionals (like
IF ... ELSE { ... }), GOTO is required to
route execution based on conditions such as file existence, string
equality, or error levels.
@ECHO OFF
IF EXIST CONFIG.SYS GOTO Found
ECHO Config file missing.
GOTO End
:Found
ECHO Config file located.
:EndIn this pattern, GOTO End acts as an exit jump to
prevent execution from "falling through" into subsequent code
blocks.
Creating Loops
MS-DOS 6.22 uses GOTO to implement iteration, often
paired with the SHIFT command to cycle through command-line
parameters.
@ECHO OFF
:Loop
IF "%1"=="" GOTO Done
ECHO Processing parameter: %1
SHIFT
GOTO Loop
:DoneIn this loop, SHIFT moves each command-line argument
down by one position. The IF statement checks whether the
current parameter %1 is empty; if not, the script loops
back to :Loop. When all parameters are exhausted, the jump
directs control to :Done, terminating the loop.