call - Execute Other Batch Files
As the range of applications for batch files expands, you will often end up with several batch files that perform similar processes, or want to call individual batch files in sequence from a batch file used for scheduled execution.
This page explains the call command, which runs another batch file from a batch file, covering basic usage, how it differs from running a file without call, passing arguments, and calling labels, with sample code.
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]
Specify the batch file name and the parameters to pass to it. The extension (.cmd or .bat) can be omitted, but writing it explicitly avoids running an unintended file when files share the same name.
Using the call command, the specified batch file is executed and returns to the calling batch file after completion.
If you write another batch file name in a batch file without call (for example, just callee.cmd), processing moves to that file and never returns to the caller. Always use call if you want the caller’s remaining commands to run.
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 calling 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 execution number %1.
echo Finished callee.cmd.
When caller.cmd is executed with the above settings, the following will be displayed:

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Receiving the exit code of the called batch file
When the called batch file ends with exit /b <number>, that value is set in ERRORLEVEL. The caller can check it right after call to determine success or failure.
@echo off
call callee.cmd
if errorlevel 1 (
echo callee.cmd failed.
exit /b 1
)
echo callee.cmd finished successfully.
For more on exit /b, see the following page.
Calling a label in the same batch file
call can also call a label in the same batch file instead of another file. Prefix the label name with :, and end the subroutine with exit /b to return to the caller.
@echo off
call :greet Taro
call :greet Hanako
exit /b
:greet
echo Hello, %1.
exit /b
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.
Summary
- To run another batch file and return to the caller, use
call - Pass arguments after the file name, as in
call callee.cmd 1, and receive them with%1in the callee - Call a label in the same file with
call :labeland return withexit /b - To run without waiting for completion, use
start