Create Directory Recursively in MS-DOS 6.22

This article explains how to create nested directory paths recursively using MS-DOS 6.22 batch routines. Unlike modern Windows command processors, MS-DOS 6.22 does not natively support creating intermediate parent directories with a single command. The sections below detail the operational limits of the native MD command and provide the exact batch routines required to construct nested directory trees automatically.

The Limitation of MD in MS-DOS 6.22

In modern Windows Command Prompt environments, the MKDIR (or MD) command automatically creates intermediate directories along a specified path. In MS-DOS 6.22, however, executing MD A\B\C returns an error if directory A or B does not already exist:

Unable to create directory

To create a deep directory tree in MS-DOS 6.22, each parent directory must be created sequentially before any subdirectories can be added.

Method 1: The FOR Loop Technique

For fixed or known paths within a batch file, the most compact native syntax utilizes a FOR loop to step through each path level:

@ECHO OFF
FOR %%D IN (C:\DIR1 C:\DIR1\DIR2 C:\DIR1\DIR2\DIR3) DO IF NOT EXIST %%D MD %%D

In this routine:

  • %%D iterates sequentially through the explicit hierarchy.
  • IF NOT EXIST prevents the command from issuing error messages if a directory level is already present.
  • MD creates the corresponding directory segment.

Method 2: Dynamic Path Building with SHIFT

To recursively build an arbitrary directory path from arguments passed to a batch file, use a loop that relies on the SHIFT command. Because DOS parameter parsing natively splits tokens on spaces, arguments must be separated by spaces rather than backslashes.

Save the following routine as MAKEDIR.BAT:

@ECHO OFF
IF "%1"=="" GOTO SYNTAX
SET BUILDPATH=%1

:LOOP
SHIFT
IF "%1"=="" GOTO FINISH
SET BUILDPATH=%BUILDPATH%\%1
IF NOT EXIST %BUILDPATH% MD %BUILDPATH%
GOTO LOOP

:SYNTAX
ECHO Usage: MAKEDIR drive: dir1 dir2 dir3 ...
GOTO END

:FINISH
SET BUILDPATH=
ECHO Directory path created successfully.

:END

How the Routine Works:

  1. SET BUILDPATH=%1 initializes the root drive or base directory (for example, C:).
  2. SHIFT moves the command-line arguments down by one position (%2 becomes %1).
  3. IF "%1"=="" GOTO FINISH terminates the loop once all tokens have been processed.
  4. SET BUILDPATH=%BUILDPATH%\%1 appends the next subdirectory token to the accumulated path string.
  5. IF NOT EXIST %BUILDPATH% MD %BUILDPATH% checks for and creates each directory level sequentially.

Execution Example:

MAKEDIR C: LEVEL1 LEVEL2 LEVEL3

This command evaluates and creates:

  1. C:\LEVEL1
  2. C:\LEVEL1\LEVEL2
  3. C:\LEVEL1\LEVEL2\LEVEL3