NT Syntax


Call one batch program from another.

      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_


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

   CALL :s_staff SMITH 100
   GOTO :eof

   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

   CALL :s_calc 200 100
   ECHO %v_return%
   GOTO :eof

   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

CALL can be used to run any internal command (SET, ECHO etc) and will evaluate any environment variables passed on the same line. Each CALL does a % substitution. (You can also do CALL CALL... for multiple substitutions)

For example

  @ECHO off
  SET v_name=simon
  SET v_myvar=v_name

  echo [%v_myvar%]
  CALL echo [%%%v_myvar%%%]

  :: note the line above is equivalent to
  :: ECHO [%[%v_myvar%]%]
  :: but when using CALL we have to double
  :: the outer %'s to prevent CALL from attempting 
  :: to evaluate this like a second environment variable

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

Simon Sheppard