BAT Files
Scripting
Batch Programming
Automation
Windows CMD

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:

batch
CALL [drive:][path]filename [batch-parameters]
  • drive: and path specify the location and name of the second batch file.
  • filename is the name of the batch file you want to execute.
  • batch-parameters are 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:

batch
1@ECHO OFF
2CALL BackupFiles.bat
3CALL CleanTemp.bat
4CALL UpdateSystem.bat
5ECHO All tasks are completed successfully!
6PAUSE

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:

batch
1@ECHO OFF
2CALL BackupFiles.bat D:\Backup
3CALL CleanTemp.bat C:\Windows\Temp
4CALL UpdateSystem.bat /force
5ECHO All tasks with parameters are completed successfully!
6PAUSE

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:

batch
1@ECHO OFF
2CALL BackupFiles.bat
3IF %ERRORLEVEL% NEQ 0 GOTO End
4CALL CleanTemp.bat
5IF %ERRORLEVEL% NEQ 0 GOTO End
6CALL UpdateSystem.bat
7IF %ERRORLEVEL% NEQ 0 GOTO End
8
9ECHO All tasks are completed successfully!
10GOTO Continue
11:End
12ECHO An error occurred during the scripts execution.
13PAUSE
14EXIT
15
16: Continue
17PAUSE

Summary Table

CommandDescriptionExample Usage
CALLExecute another batch fileCALL anotherfile.bat
ECHODisplay message or turn off command echoingECHO OFF or ECHO Hello
PAUSEWait for user inputPAUSE
%ERRORLEVEL%System variable for exit status of a commandIF %ERRORLEVEL% NEQ 0

Advanced Topics

  • Looping through Files: You can use loops (e.g., FOR loops) 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.


Course illustration
Course illustration

All Rights Reserved.