Loop Through Files Using FOR IN DO in MS-DOS 6.22

In MS-DOS 6.22, batch files automate repetitive tasks across multiple files by using the FOR...IN...DO command. This article explains the exact syntax required by the legacy COMMAND.COM interpreter, how to handle single-character loop variables, and practical examples for iterating over specific file sets and wildcards within the limitations of the 8.3 filename system.

Basic Syntax

To loop through files inside an MS-DOS 6.22 batch file (.BAT), you must use the FOR statement with a double-percent sign (%%) preceding a single-letter variable:

FOR %%variable IN (set) DO command [arguments]
  • %%variable: A single-character variable name (e.g., %%f, %%a). In MS-DOS 6.22, variable names are case-sensitive and must consist of only one character. The double percent sign prevents DOS from treating it as a standard batch parameter (like %1).
  • (set): A space-separated or comma-separated list of filenames, which can include the standard DOS wildcards * and ?. The parentheses are mandatory.
  • DO command: The DOS command executed on each iteration. %%variable is replaced by the current file name during execution.

Note: If you run this command directly from the MS-DOS command prompt instead of inside a batch file, use a single percent sign (e.g., %f) instead of double percent signs (%%f).

Looping Through Files with Wildcards

The most common use case is processing all files matching a specific pattern in the current directory.

For example, to display the contents of every text file sequentially:

@ECHO OFF
FOR %%F IN (*.TXT) DO TYPE %%F

To copy every .DAT file in the current directory to a backup directory:

@ECHO OFF
FOR %%F IN (*.DAT) DO COPY %%F C:\BACKUP\

Looping Through an Explicit List of Files

You can specify a predefined list of distinct files inside the parentheses instead of using wildcards:

@ECHO OFF
FOR %%F IN (CONFIG.SYS AUTOEXEC.BAT COMMAND.COM) DO COPY %%F A:\

DOS processes each item in the set from left to right until the list is exhausted.

Limitations in MS-DOS 6.22

When writing batch scripts for MS-DOS 6.22, keep the following environment constraints in mind:

  1. No Modern Extensions: Flags introduced in Windows NT and later—such as /R (recursive), /F (parsing text), and /L (numeric ranges)—do not exist in MS-DOS 6.22.
  2. No Multi-Line Parentheses: You cannot execute a block of multiple commands wrapped in parentheses after DO. To execute multiple commands per file, use CALL to run a secondary batch file, passing %%F as an argument:
    FOR %%F IN (*.DOC) DO CALL PROCESS.BAT %%F
  3. 8.3 Filename Format: Filenames are strictly limited to eight characters for the name and three characters for the extension.