how to use CALL command

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.

CALL a second batch file

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%
Example
::-------------start main.cmd------------------
@Echo off
CALL function.cmd 10 first
Echo %_description% - %_number%

CALL function.cmd 15 second
Echo %_description% - %_number%
::-------------end main.cmd--------------------

::-------------start function.cmd--------------
@Echo off
:: Add 25 to %1
SET /a _number=%1 + 25
:: Store %2
SET _description=[%2]
::-------------end function.cmd----------------
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.

CALL a subroutine (:label)

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
ECHO Done
GOTO :eof
:sub_display
ECHO The result is %1
GOTO :eof
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.

Passing by Reference

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.

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

Advanced usage : CALLing internal commands

    CALL command [command_parameters]
CALL can also be used to run any internal command (SET, ECHO etc) with the exception of FOR, IF and REM.
CALL will expand any environment variables passed on the same line. 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.

Example
   @ECHO off
   SETLOCAL
   set server1=frodo3
   set server2=gandalf4
   set server3=ascom5
   set server4=last1
   
   ::run the Loop for each of the servers
   call :loop server1
   call :loop server2
   call :loop server3
   call :loop server4
   goto:eof
   
   :loop
   set _var=%1
   :: Evaluate the server name
   CALL SET _result=%%%_var%%%
   echo The server name is %_result%
   goto :eof
   
   :s_next_bit
   :: continue below

:: Note the line shown in bold has three '%' symbols
:: The CALL will expand this to: SET _result=%server1% 
Each CALL does one substitution of the variables. (You can also do CALL CALL... for multiple substitutions)
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.
CALL is an internal command. If Command Extensions are disabled, the CALL command will not accept batch labels. 

Comments

Popular Posts