call - Execute Other Batch Files
As the range of applications for batch files expands, it is common to have multiple batch files that perform similar processes within a program.
In such cases, it is inefficient to write the same process in multiple batch files, and maintenance costs increase.
Also, it is common to call multiple batch files created separately from a batch file for scheduled execution.
This page explains how to execute other batch files from a batch file, from basic usage to setting options, in an easy-to-understand manner.
Basic usage of the call command
To call other batch files from a batch file, use the call
command.
The basic usage of the call
command is as follows.
call <batch file name> [...parameters]
There are no options, only the batch file name and the parameters to pass to the batch file are specified.
Using the call
command, the specified batch file is executed and returns to the calling batch file after completion.
Examples of the call command
Executing other batch files
As an example, prepare the caller.cmd
to be executed directly and the callee.cmd
to be called.
caller.cmd
looks like this:
@echo off
echo Started caller.cmd.
call callee.cmd
echo Finished caller.cmd.
Similarly, callee.cmd
looks like this:
@echo off
echo Started callee.cmd.
echo Finished callee.cmd.
When caller.cmd
is executed with the above settings, the following will be displayed:
When caller.cmd
is executed, callee.cmd
is executed by the call
command, and after callee.cmd
is completed, it returns to caller.cmd
.
In this way, when using the call
command, the called batch file waits until the called batch file is completed.
Executing a batch file by passing arguments
Next, we will explain how to execute a batch file by passing arguments to it.
Prepare caller.cmd
to be executed directly and callee.cmd
to be called.
caller.cmd
looks like this:
@echo off
echo Started caller.cmd.
call callee.cmd 1
call callee.cmd 2
echo Finished caller.cmd.
callee.cmd
is written as follows:
@echo off
echo Started callee.cmd.
echo This is the first execution.
echo Finished callee.cmd.
When caller.cmd
is executed with the above settings, the following will be displayed:
If you want to achieve parallel processing
There may be cases where you want to execute the next process without waiting for the completion of the called batch file when calling a batch file.
If you use the call
command, the next process will be executed after waiting for the completion of the called batch file.
If you want to execute the next process without waiting for completion, use the start
command.
For more information on the start
command, see the following page.