Call one batch program from another, or call a subroutine.
Syntax CALL [drive:][path]filename [parameters] CALL :label [parameters] CALL internal_cmd Key: pathname The batch program to run. parameters Any command-line arguments. :label Jump to a label in the current batch script. internal_cmd Run an internal command, first expanding any variables in the argument.
The CALL command will launch a new batch file context along with any specified parameters.
When the end of the second batch file is reached (or if EXIT is used), control will return to just after the initial CALL statement.
Arguments can be passed either as a simple string or using a variable:
CALL MyScript.cmd "1234"
CALL OtherScript.cmd %_MyVariable%
CALL function.cmd 10 first
Echo %_description% - %_number%
CALL function.cmd 15 second
Echo %_description% - %_number%
:: Add 25 to %1
SET /a _number=%1 + 25
:: Store %2
In many cases you will also want to use SETLOCAL and ENDLOCAL to keep variables in different batch files completely separate, this will avoid any potential problems if two scripts use the same variable name.
The CALL command will pass control to the statement after the label specified along with any specified parameters.
To exit the subroutine specify GOTO:eof this will transfer control to the end of the current subroutine.
A label is defined by a single colon followed by a name. This is the basis of a batch file function.
CALL :sub_display 123
CALL :sub_display 456
ECHO All Done
ECHO The result is %1
At the end of the subroutine, GOTO :eof will return to the position where you used CALL.
Command blocks do not work with the call command.
Redirection with & | <> also does not work as expected.
If the CALL command contains a caret character within a quoted string "test^ing" , the carets will be doubled.
In addition to passing numeric or string values on the command line, it is also possible to pass a variable name and then use the variable to transfer data between scripts or subroutines. Passing by reference is a slightly more advanced technique but can be particularly useful when the string contains characters that are CMD delimiters or quotes.
@Echo off Echo. Set "var1=Red Pippin" Set "var2=St Edmunds Pippin" Set "var3=Egremont Russet" Echo. before: var1=%var1% var2=%var2% var3=%var3% call :myGetFunc var1 var2 var3 Echo. after: var1=%var1% var2=%var2% var3=%var3% Echo.&pause&goto:eof ::---------------------------------------------- ::-- Function section starts below ::---------------------------------------------- :myGetFunc - passing a variable by reference Set "%~1=return64" Set "%~3=return65" Goto:eof
CALL command [command_parameters]
CALL can also be used to run any internal command (SET, ECHO etc) with the exception of FOR and IF.
CALL will expand any variables passed on the same line. CALL REM only partly works: redirection operators, conditional execution operators and brackets will be not remarked.
This is undocumented behaviour, in fact whenever CALL is run without a : prefix, it will always search disk for a batch file/executable called command before running the internal command. The effect of this extra disc access is that CALL SET is significantly slower than CALL, its use in loops or with a large number of variables should be avoided.
@Echo off SETLOCAL set _server=frodo set _var=_server CALL SET _result=%%%_var%%% echo %_result%
The line shown in bold has the '%' symbols tripled, CALL will expand this to: SET _result=frodo
Each CALL does one substitution of the variables. (You can also do CALL CALL... for multiple substitutions)
In many cases, DelayedExpansion is a better/faster method:
@Echo Off Setlocal EnableDelayedExpansion Set _server=frodo Set _var=_server Set _result=!%_var%! Echo %_result%
If you CALL an executable or resource kit utility make sure it's available on the machine where the batch will be running, test for it's existence with an IF command and throw an error if missing.
CALL is an internal command. 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
Syntax: Functions - How to package blocks of code.
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 bash command (Linux): . (source) - Run a command script in the current shell, builtin - Run a shell builtin.