Running Multiple Batch Files Sequentially from a Single Batch File

Introduction

Batch scripting is a powerful tool for automating repetitive tasks on Windows operating systems. One common requirement is to run multiple batch files sequentially as part of a larger workflow or build process. This tutorial will guide you through various methods to execute multiple .BAT files from within a single batch file, ensuring each script runs in sequence and handles any errors gracefully.

Basic Execution with CALL

The simplest method to ensure sequential execution of batch files is using the CALL command. Without CALL, executing one batch file from another stops the calling batch file, transferring control entirely to the called batch file. This behavior can disrupt your workflow if you need multiple scripts to run in order.

Example

@echo off
call msbuild.bat
call unit-tests.bat
call deploy.bat

This approach ensures that msbuild.bat completes before starting unit-tests.bat, and so on, until all specified batch files have been executed.

Handling Spaces in File Names

If your batch file names contain spaces, ensure you enclose them in quotes to prevent errors:

call "unit tests.bat"

This syntax ensures that the command interpreter recognizes the entire filename correctly.

Error Handling with ERRORLEVEL

You can manage error conditions by checking the ERRORLEVEL of each script. This allows for conditional execution based on whether a script ran successfully or encountered an error.

Example

call msbuild.bat
if %errorlevel% neq 0 exit /b %errorlevel%

call unit-tests.bat
if %errorlevel% neq 0 (
    echo Unit tests failed.
    exit /b %errorlevel%
)

call deploy.bat

In this example, the script checks if msbuild.bat or unit-tests.bat exits with an error. If so, it outputs a message and stops further execution.

Using FOR Loop for Dynamic Execution

If you have multiple batch files in a directory and wish to execute them dynamically, use a FOR loop:

for %%f in (*.bat) do (
    call "%%f"
)

This script will iterate over all .BAT files in the current directory and execute each one sequentially.

Advanced Execution Techniques

Using Start Command

For executing scripts in separate command windows without waiting for completion, use the START command:

start "" cmd /c msbuild.bat
start "" cmd /c unit-tests.bat

This method is useful when you want to run background tasks or don’t need to wait for a script to finish before starting another.

Using Conditional Execution

For conditional execution based on the success of previous commands, use && and ||:

msbuild.bat && unit-tests.bat || echo Build failed!

This command runs unit-tests.bat only if msbuild.bat succeeds. If it fails, an error message is displayed.

Using Subprocesses

For more control over subprocess execution, use tools like WMIC, SCHTASKS, or third-party utilities like ScriptRunner. These allow for advanced features such as monitoring process IDs, scheduling tasks, and handling rollback on errors.

Example with WMIC

for /f "tokens=2 delims==;" %%# in ('WMIC process call create "%cd%\first.bat", "%cd%" ^| find "ProcessId"') do (
    set "PID=%%#"
)
echo Process ID: %PID%

This snippet launches a batch file as a separate process and captures its process ID for monitoring.

Conclusion

Running multiple batch files sequentially from a single script can be achieved using various methods, each with its own advantages. Whether you need simple sequential execution or advanced error handling and process control, the techniques discussed here provide robust solutions to automate your workflows effectively.

Leave a Reply

Your email address will not be published. Required fields are marked *