19

It seems that a cmd script containing:

prog1
prog2

does the same as

call prog1
call prog2

What is the point of using the CALL command ?

Martin
  • 9,674
  • 5
  • 36
  • 36

3 Answers3

24

You should use call when you either want to:

  • call another command file and return to this one when it's done. ; or
  • call a function in the current command file.

A command file with the line:

number2.cmd

will chain to the number2.cmd file, meaning it will run that script but not return to continue execution on the current one.

As to the second point, you can do things like:

call :subroutine
call :subroutine
goto :eof

:subroutine
    echo in here
    goto :eof

and you will get in here printed twice. This ability to call functions within command scripts is actually quite handy.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • @paxdiablo, What do you mean by "*chain* to the `number2.cmd` file"? – Pacerier Aug 25 '15 at 21:22
  • @Pacerier, I meant it won't return to the current script when done, a phrase I've held on to since my BASIC programming days too many decades ago :-) See https://en.m.wikipedia.org/wiki/Chain_loading for details. I've hopefully made that clearer with the edit. – paxdiablo Aug 25 '15 at 22:27
15

You should use call when you need to call another batch program (cmd script). Using 'call' will have no effect if prog1 is an executable file. (prog1.exe)

If you, for example, have two scripts:

cmd1.cmd
cmd2.cmd

And within cmd1.cmd you have a line:

cmd2.cmd

... then your script will stop as soon as cmd2.cmd is finished executing. Instead you should use:

call cmd2.cmd
mpontillo
  • 13,559
  • 7
  • 62
  • 90
  • Aha - so the distinction between external executables and external cmd scripts is the key. Thanks for the enlightenment. – Martin Jan 28 '11 at 07:50
  • 1
    This is incorrect. If you use "call" for an executable, the executing comes back to the script after the executable is done. If you do not use call, the entire thing ends when the executable is completed, even if you have further commands in the script after that. – user118967 Feb 03 '19 at 17:11
3

Normally call is used to run another batch file within a batch file. When the batch file that is called is completed, the remainder of the original batch file is completed.

Note if the batch file does not exist it will give an error message.

syntax is: CALL [drive:][path]filename [batch-parameters]

There are no restriction in where to call it. You can use CALL command in any batch file to call another batch file.

Hope this helps.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207