|
|
|
CALL
Call one batch program from another.
syntax
CALL [drive:][path]filename [parameters]
CALL :label [parameters]
CALL internal_cmd
key:
parameters Any command-line arguments.
:label Jump to a label somewhere within the
current batch file.
internal_cmd Any internal NT command
CALLing a command in this way (rather
than simply running it) will evaluate
any environment variable parameters
note: CALL does support network (UNC) pathnames
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.
Jumping to a label
A label is defined by a single colon followed by a name
As this is effectively a subroutine I always prefix the name with s_
:s_error_message
When you jump to a subroutine with CALL, all statements after the label are
treated like a separate batch file. When the end of the batch is reached, it
will exit and control will return back to just after the position where you
used CALL.
You can therefore reach the end of the batch script file more than once.
Don't forget you can also pass command line arguments to the :label and refer
to these just like the parameters passed to a separate batch file.
For example
@ECHO OFF SETLOCAL CALL :s_staff SMITH 100 GOTO :eof :s_staff SETLOCAL ECHO Name is %1 ECHO Rate is %2 GOTO :eof
Notice that it is not necessary to place "quotes" around
any of the values.
Returning Parameters
When a subroutine is complete you need a method of returning values, i.e.
setting a variable that is visible 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 %v_return% GOTO :eof :s_calc SETLOCAL SET v_sum=0 IF %1 GTR %2 SET v_sum=5 ENDLOCAL & SET v_return=%v_sum% GOTO :eof
Another example of this. This technique
works in exactly the same way when calling a subroutine.
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 v_pc_name=pc%1 :: Evaluate the PC's name CALL SET v_pc_name=%%%v_pc_name%%% echo The pc is %v_pc_name% goto :eof :s_next_bit :: continue below :: Notice that to evaluate the contents of %pc1% :: requires triple '%' symbols i.e CALL SET v_pc_name=%%%v_pc_name%%%
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