cmd.exe: when to use call to run external programs
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 ?
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.
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
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.
精彩评论