Pass Variables to Secondary Scripts in MS-DOS 6.22
In MS-DOS 6.22, passing data from a primary batch file to a secondary
batch script is primarily accomplished through two methods: passing
values as positional command-line arguments using the CALL
statement, or sharing global environment variables initialized with the
SET command. Because MS-DOS executes nested batch files
within the same command processor instance when invoked via
CALL, both methods allow the child script to read and
process the caller's data reliably.
Method 1: Command-Line Arguments via the CALL Command
The standard and most modular method to pass variables to a secondary
script is using positional parameters with the CALL
command.
In the primary script, invoke the secondary script using
CALL, appending any variables you wish to pass:
@echo off
set USERNAME=JohnDoe
set USERID=42
call SECOND.BAT %USERNAME% %USERID%In SECOND.BAT, the passed arguments are automatically
mapped to positional parameters ranging from %1 to
%9:
@echo off
echo Processing user: %1
echo User ID: %2If you need to pass more than nine parameters, the secondary script
must use the SHIFT command to slide the parameter window
down, allowing %9 to read subsequent arguments. Using the
CALL command ensures that once the secondary script
finishes execution, control returns to the next line of the calling
script.
Method 2: Shared Environment Variables
MS-DOS 6.22 maintains a single, flat environment table for the active
COMMAND.COM session. When a primary script defines an
environment variable using the SET command, that variable
remains available in memory to any secondary script launched via
CALL.
Primary script (PARENT.BAT):
@echo off
set TARGET_DIR=C:\BACKUP
call CHILD.BATSecondary script (CHILD.BAT):
@echo off
echo Target directory is: %TARGET_DIR%Unlike command-line parameters, variables modified or created inside
CHILD.BAT will persist and affect PARENT.BAT
after CHILD.BAT terminates.
Important MS-DOS 6.22 Constraints
- Execution Flow: Always use the
CALLcommand (e.g.,call SUB.BAT) rather than simply executing the script name (SUB.BAT). Calling a script by name withoutCALLtransfers control permanently, terminating the parent script immediately. - Environment Memory Limits: MS-DOS allocates a
default environment table size of only 256 bytes. If you pass large
amounts of data using environment variables, you may encounter the "Out
of environment space" error. This limit can be increased by modifying
the
SHELLline inCONFIG.SYSusing the/E:switch (for example,SHELL=C:\DOS\COMMAND.COM C:\DOS\ /E:1024 /P).