Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several ways to call a windows batch file from another one or from prompt. Which one in which case?

A windows batch file (called.bat or called.cmd) can be called from another batch file (caller.bat or caller.cmd) or interactive cmd.exe prompt in several ways:

  1. direct call: called.bat
  2. using call command: call called.bat
  3. using cmd command: cmd /c called.bat
  4. using start command: start called.bat

I'm quite in trouble to differentiate their intended usage based on their help text: when to use which one? e.g. why I might use 'call' command instead of direct call. What's different?

I'm interested on some summary report that analyze all 4 possibilities (and others if any missing) from various point of views: recommended use cases for which they are designed to fit, process spawning, execution context, environment, return code processing.

Note: I'm using Windows XP SP3.

like image 924
dim Avatar asked Apr 09 '10 13:04

dim


People also ask

Can you call a batch file from another batch file?

Simply entering a batch file's name within another batch file will run the batch file you want to call. However, after the called batch file completes, it won't pass control back to the calling batch file. Thus, the calling batch file will be incomplete.

How do you call a batch file?

To run a batch file, move to the directory containing the file and type the name of the batch file. For example, if the batch file is named "hope. bat," you'd type "hope" to execute the batch file.

What is call command in batch file?

The call command enables a user to execute a batch file from within another batch file.


2 Answers

  1. The batch file will be executed by the current cmd.exe instance (or a new cmd.exe instance if, for instance, double-clicked in Explorer).

  2. Same as #1, only has an effect when used inside a batch/cmd file. In a batch file, without 'call', the parent batch file ends and control passes to the called batch file; with 'call' runs the child batch file, and the parent batch file continues with statements following call.

  3. Runs the batch file in a new cmd.exe instance.

  4. Start will run the batch file in a new cmd.exe instance in a new window, and the caller will not wait for completion.

like image 74
Kyle Alons Avatar answered Sep 21 '22 17:09

Kyle Alons


One thing not clear from the comments here: When you call one batch file from another by using just its name (Case #1 in the original question), execution stops from the calling batch file. For example, in these lines:

called.bat echo Hello 

The 'echo Hello' line (and anything following it) will not be called. If you use the 'call' keyword, execution resumes after the call. So in this case:

call called.bat echo Hello 

The 'echo Hello' line will be called.

Additionally, all the variables set in the called.bat file will be passed along back to the calling process, too.

Imagine a 'called.bat' file that had this line:

set MYVAR=hello 

Then, %MYVAR% would be available to the calling batch file if it used:

call called.bat 

But, it would not be using

REM starts a new cmd.exe process start called.bat     REM stops and replaces current cmd.exe process with a new one called.bat         
like image 36
ken Avatar answered Sep 19 '22 17:09

ken