Purpose: | Execute one batch file from within another. |
Format: | CALL file | :label [p1 [p2 ...]] |
file | The batch file to execute. |
:label | A label in the current batch file. |
p1, p2,... | Parameters for the batch file or subroutine |
Usage:
CALL allows batch files to call other batch files (batch file nesting). The calling batch file is suspended while the called (second) batch file runs. When the second batch file finishes (without executing the CANCEL command), execution of the original batch file resumes at the next command.
WARNING! If you execute a batch file from inside another batch file without using CALL, the original batch file is terminated before the other one starts. This method of invoking a batch file from another is usually referred to as chaining. Note that if the batch file A.BTM uses CALL B, and B.BTM chains to the batch file C.BTM, on exit from C.BTM (without executing a CANCEL command) processing of batch file A.BTM is resumed as if it had used CALL C.
File A.BTM:
...
call b
echo xxx
File B.BTM:
...
C
File C.BTM:
...
quit
In the example above, after execution of the QUIT command in C.BTM the ECHO xxx command in A.BTM is executed next.
The following batch file fragment compares an input line to wp and calls another batch file if it matches:
input Enter your choice: %%option
if "%option" == "wp" call wp.bat
Batch files may be nested up to 64 levels deep.
The current ECHO state is inherited by a called batch file.
The called batch file should always either return (by executing its last line, or by using the QUIT command), or it should terminate batch file processing with CANCEL. Do not restart or CALL the original batch file from within the called file as this may cause an infinite loop or a stack overflow.
To provide compatibility with CMD, which does not support the GOSUB command for subroutines in the same batch file, you may create a subroutine starting with a label and terminated by any of the following:
•the end of the batch file
•QUIT
•EXIT
Note that the last two do NOT return control to the CALL command. Do not use the RETURN command!
Parameters passed to the subroutine are accessible as %1, %2, etc., in the same manner as in a batch file.
CALL returns an exit code which matches the batch file return code. You can test this exit code with conditional commands (&& and ||).
See also GOSUB and user-defined functions.