Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To "Call" or "Not to Call" a batch file?

If from inside a bat file you called another batch file but still had a few remaining operations to complete, how can you make sure that the call to first bat file will after completion or error, will return to the file that called it in the first instance?

Example:

CD:\MyFolder\MyFiles Mybatfile.bat  Copy afile toHere 

or

CD:\MyFolder\MyFiles CALL Mybatfile.bat  COPY afile toHere 

What is the difference between using CALL or START or none of them at all? Would this have any impact on whether it would return for the results of the copy command or not?

like image 402
AltF4_ Avatar asked Feb 06 '13 14:02

AltF4_


People also ask

What does call do in batch file?

Purpose: Calls another batch file and then returns to the current batch file to continue processing. Used within a batch file to specify the name of another batch file (a file with the . BAT filename extension).

What does 0 |% 0 Do in batch?

What does 0 |% 0 Do in batch? %0|%0 is a fork bomb. It will spawn another process using a pipe | which runs a copy of the same program asynchronously. This hogs the CPU and memory, slowing down the system to a near-halt (or even crash the system).

What is @echo off batch?

When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.

Why is it called a batch file?

Also known as a batch job, a batch file is a text file created in Notepad or some other text editor. A batch file bundles or packages a set of commands into a single file in serial order. Without a batch file these commands would have to be presented one at a time to the system from a keyboard.


1 Answers

As others have said, CALL is the normal way to call another bat file within a .bat and return to the caller.

However, all batch file processing will cease (control will not return to the caller) if the CALLed batch file has a fatal syntax error, or if the CALLed script terminates with EXIT without the /B option.

You can guarantee control will return to the caller (as long as the console window remains open of course) if you execute the 2nd script via the CMD command.

cmd /c "calledFile.bat" 

But this has a limitation that the environment variables set by the called batch will not be preserved upon return.

I'm not aware of a good solution to guarantee return in all cases and preserve environment changes.

If you really need to preserve variables while using CMD, then you can have the "called" script write the variable changes to a temp file, and then have the caller read the temp file and re-establish the variables.

like image 170
dbenham Avatar answered Oct 11 '22 19:10

dbenham