How to run multiple .BAT files within a .BAT file
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Batch files (.BAT) are script files in DOS, Windows, and OS/2 operating systems that consist of a series of commands executed by the command-line interpreter. Using .BAT files can simplify repetitive or routine tasks. An advanced usage of batch files is running multiple .BAT files from within a single .BAT file. This can be particularly useful for orchestrating a series of scripts for various purposes like backups, system checks, or batch processing of files.
Running Multiple .BAT Files
To execute one batch file from another, you use the CALL command. CALL is used to invoke another batch file and then return to the original batch file and continue executing the next line of code. Without CALL, the control would not return to the original batch file, and it would simply stop executing after the called batch file has finished.
Basic Syntax
The basic syntax of the CALL command is:
drive:andpathspecify the location and name of the second batch file.filenameis the name of the batch file you want to execute.batch-parametersare the command line parameters if any are needed.
Example
Consider a scenario where you have three batch files: BackupFiles.bat, CleanTemp.bat, and UpdateSystem.bat. You want to run all these from a master batch file named MasterScript.bat.
MasterScript.bat:
This script sequentially calls three other scripts. It begins with @ECHO OFF to keep the command line output clean, followed by the CALL statements for each task-specific batch file. After all scripts have run, it prints a completion message and waits for any key press before closing.
Handling Parameters
Often, the called batch files require input parameters. You can pass parameters from the master batch file to the child batch files.
MasterScript.bat with parameters:
Here, D:\Backup, C:\Windows\Temp, and /force are parameters passed to the respective batch files to guide their execution.
Error Handling
Error handling can be incorporated to ensure each script is executed successfully before moving on to the next script. You can check the exit code of each batch file using %ERRORLEVEL%.
MasterScript.bat with error handling:
Summary Table
| Command | Description | Example Usage |
CALL | Execute another batch file | CALL anotherfile.bat |
ECHO | Display message or turn off command echoing | ECHO OFF or ECHO Hello |
PAUSE | Wait for user input | PAUSE |
%ERRORLEVEL% | System variable for exit status of a command | IF %ERRORLEVEL% NEQ 0 |
Advanced Topics
- Looping through Files: You can use loops (e.g.,
FORloops) in your master batch file to iterate through a list of batch files dynamically. - Scheduled Execution: Integrate with Windows Task Scheduler to run your master batch file at specific times.
- Integration with Other Scripts: Beyond batch files, a master batch can also call PowerShell scripts using
PowerShell.exe -File "script.ps1".
Executing .BAT files within a master .BAT file effectively automates and streamlines multiple script operations, enhancing productivity and reducing the possibility of human error in manual executions.

