CALL
Call one batch program from another.
Syntax CALL [drive:][path]filename [parameters]
CALL :label [parameters] CALL internal_cmd Key: pathname The batch program to run this can be a network (UNC) pathname parameters Any command-line arguments.
:label Jump to a label in the current batch script. internal_cmd Any internal command CALLing a command in this way (rather than simply running it) will evaluate any environment variable parameters
Passing Parameters
When calling a secondary batch file or subroutine, you will often want the routine
to manipulate some data, the data (usually a variable) should be passed as a
parameter
CALL OtherScript.cmd "1234"
or
CALL OtherScript.cmd %_MyVariable%
Use a label to CALL a subroutine
A label is defined by a single colon followed by a name.
CALL :s_display_result 123
ECHO Done
GOTO :eof
:s_display_result
ECHO The result is %1
GOTO :eof
When you jump to a subroutine with CALL, all statements after the label are
executed until either the end of the script is reached, or a GOTO :eof command.
At the end of the subroutine, GOTO :eof
will return to the position where
you used CALL.
Example
@ECHO OFF SETLOCAL CALL :s_staff SMITH 100 GOTO s_last_bit :s_staff ECHO Name is %1 ECHO Rate is %2 GOTO :eof :s_last_bit ECHO The end of the script
Returning Parameters
When a subroutine contains local variables (SETLOCAL)
you will need a method of returning values, i.e. setting a variable that is
passed back to the
calling
routine.
This is done by executing the ENDLOCAL command on the same line as a SET statement(s)
For example
@ECHO OFF SETLOCAL CALL :s_calc 200 100 ECHO %_return% GOTO :eof :s_calc SETLOCAL SET _sum=0 IF %1 GTR %2 SET _sum=5 ENDLOCAL & SET _return=%_sum% GOTO :eof
The use of SETLOCAL and ENDLOCAL is roughly equivalent to option explicit in Visual Basic, it's use is strongly recommended.
You should
also use SETLOCAL and ENDLOCAL when passing values
from one batch file to another.
Advanced usage : CALLing internal commands
As well as running a subroutine, CALL can also be used to run any internal
command (SET, ECHO etc) and cruicially will evaluate any environment variables
passed on the same line.
Each CALL does one substitution of the variables. (You can also
do CALL CALL... for multiple substitutions)
For example
@ECHO off SETLOCAL set pc1=frodo3 set pc2=gandalf4 set pc3=ascom5 set pc4=qwerty2 set pc5=last1 ::Loop through all the PCs FOR /L %%n IN (1,1,5) DO (call :loop %%n) goto :s_next_bit :loop set _pc_name=pc%1 :: Evaluate the PC's name CALL SET _pc_name=%%%_pc_name%%% echo The pc is %_pc_name% goto :eof :s_next_bit :: continue below :: Notice that to evaluate the contents of %pc1% :: requires triple '%' symbols i.e CALL SET _pc_name=%%%_pc_name%%%
If you CALL an executable or resource kit utility make sure it's available on the machine where the batch will be running, also check you have the latest versions of any resource kit utilities.
If Command Extensions are disabled, the
CALL command will not accept batch labels.
"My mother never saw the irony in calling me a son-of-a-bitch." - Jack
Nicholson
Related commands:
CMD - can be used to call a subsequent batch and ALWAYS
return even if errors occur.
GOTO - jump to a label or GOTO :eof
START - Start a separate window to run a specified
program or command
Equivalent Linux BASH commands:
. (dot operator) - Include (run) commands
from another file
builtin - Run a shell builtin
chroot - Run a command with a different root
directory