Error DataBase-One Place all Solutions Forums Blog Glossary    Contact Us
Search  
   
Browse by Category
Error DataBase-One Place all Solutions .: Operating Systems .: Windows Operating Systems .: MS DOS .: How to Call one batch program from another using CALL command

How to Call one batch program from another using CALL command

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 parameters Any command-line arguments
:label Jump to a label in the current batch script. internal_cmd Any 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 arguments

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.

CALL a subroutine (:label)
The CALL command will pass control to the statement after the label specified along with any specified arguments .
To exit the subroutine specify GOTO:eof this will transfer control to the end of the current subroutine.

Arguments can be passed either as a simple string or using a variable:

CALL MyScript.cmd "1234"
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

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

SETLOCAL will make subroutine variables invisible

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 can be done by with an ENDLOCAL command on the same line as a SET statement(s)

For example

   @ECHO OFF
   SET _sum=64000
   CALL :s_calc 200 100
   Echo %_return%
   Echo %_sum%
   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 recommended.

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 server1=frodo3
   set server2=gandalf4
   set server3=ascom5
   set server4=qwerty2
   set server5=last1
   
   ::run the Loop for each of the 5 servers
   call :loop server1
   call :loop server2
   call :loop server3
	call :loop server4
	call :loop server5
   goto:eof
   
   :loop
   set _server=%1
   :: Evaluate the servers name
   CALL SET _server=%%%_server%%%
   echo The server is %_server%
   goto :eof
   
   :s_next_bit
   :: continue below

:: Notice that to evaluate the contents of %server1%
:: requires triple '%' symbols i.e CALL SET _server=%%%_server%%%

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.


How helpful was this article to you?

Related Articles

article How to Direct a batch program to jump to a labelled line using GOTO command
Direct a batch program to jump to a...

(No rating)  3-9-2008    Views: 156   
article How to Display or change the link between a FileType and an executable program using FTYPE command
Display or change the link between a...

(No rating)  3-9-2008    Views: 154   
article How to Quit CMD.EXE or the current batch script using EXIT command
Quit CMD.EXE or the current batch...

(No rating)  3-7-2008    Views: 133   

User Comments

Add Comment
No comments have been posted.