CALL - CMD
Overview
The CALL
command in Windows Command Prompt is used to invoke a batch file from another batch file. This allows for modular scripting, making complex batch scripts easier to manage by breaking them down into reusable parts. It’s extensively used in situations where repetitive script segments need to be executed multiple times from multiple points within a batch file or from different batch files.
Syntax
The basic syntax of the CALL
command is as follows:
CALL [drive:][path]filename [batch-parameters]
[drive:][path]filename
specifies the location and name of the batch file you want to call.[batch-parameters]
are optional parameters passed to the batch file.
Calling a batch file within another batch file without causing the parent script to stop:
CALL :label arguments
:label
specifies a label in a batch file.arguments
are any arguments you want to pass to the label.
Options/Flags
The CALL
command does not have typical flags or options, but it does handle passing parameters to scripts and jumping to labels within scripts. The command modifies the behavior as follows:
- Calling External Batch Files: Simplifies code management by keeping scripts modular and reusable.
- Jumping to Labels: Avoid restarting the script and continue from a specified point using labels in scripts.
Examples
Example 1: Basic use of CALL to run another batch file
CALL second_script.bat
This command calls second_script.bat
, executing it as if its contents were present in the caller script.
Example 2: Passing arguments to a called batch file
CALL other_script.bat 123 ABC
Executes other_script.bat
passing the arguments 123
and ABC
to it.
Example 3: Using CALL to jump to a label within the same batch file
CALL :subroutine
ECHO Back to main script.
GOTO :EOF
:subroutine
ECHO Inside the subroutine.
GOTO :EOF
This setup allows a script to handle subroutines internally and return to the main flow after the subroutine is processed.
Common Issues
- Script termination on error: If the called script encounters an error and terminates, control returns to the original script which continues execution. This can lead to unexpected behaviors if not handled properly.
- Parameter issues: Incorrect parameter passing can lead to logical errors in scripts. Always verify parameters in the receiving script.
Integration
The CALL
command is essential in script modularization. Here’s an example integrating CALL
with conditional logic:
IF EXIST setup.bat CALL setup.bat
IF NOT %ERRORLEVEL% EQU 0 ECHO setup failed!
This checks for the existence of setup.bat
, runs it if found, and checks the result before proceeding.
Related Commands
- GOTO: Directs the CMD interpreter to a labeled line in a batch file.
- EXIT: Exits the current batch script and returns control to the command interpreter.
For further reading and more detailed information, refer to the official Windows CMD documentation.