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:
%%Diterates sequentially through the explicit hierarchy.IF NOT EXISTprevents the command from issuing error messages if a directory level is already present.MDcreates 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:
SET BUILDPATH=%1initializes the root drive or base directory (for example,C:).SHIFTmoves the command-line arguments down by one position (%2becomes%1).IF "%1"=="" GOTO FINISHterminates the loop once all tokens have been processed.SET BUILDPATH=%BUILDPATH%\%1appends the next subdirectory token to the accumulated path string.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:
C:\LEVEL1C:\LEVEL1\LEVEL2C:\LEVEL1\LEVEL2\LEVEL3