Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmd.exe: when to use call to run external programs

Tags:

cmd

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 ?

like image 451
Martin Avatar asked Jan 28 '11 07:01

Martin


People also ask

How do I run a file in cmd?

You can run the commands stored in a CMD file in Windows by double-clicking the file or executing it in the Command Prompt (CMD. EXE) utility. You cannot run CMD files in COMMAND.COM, like you may do with BAT files, so that you do not incorrectly execute commands in the wrong Windows environment.

Is cmd.exe the same as Command Prompt?

Windows Command Prompt (also known as the command line, cmd.exe or simply cmd) is a command shell based on the MS-DOS operating system from the 1980s that enables a user to interact directly with the operating system.

How do I run an EXE from command line arguments in Windows?

You can test command line arguments by running an executable from the "Command Prompt" in Windows or from the "DOS prompt" in older versions of Windows. You can also use command line arguments in program shortcuts, or when running an application by using Start -> Run. This will start notepad with a blank document.


2 Answers

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.

like image 197
paxdiablo Avatar answered Oct 30 '22 13:10

paxdiablo


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
like image 20
mpontillo Avatar answered Oct 30 '22 13:10

mpontillo