How to call a batch file from within another batch file

Have you tried running an external batch file from within a batch file or script, only to find out that the original batch file script terminated as soon as the external batch script is executed?

Abnormal Batch Termination

Here’s an example.

Let’s say we have a main script called “mainscript.bat” and two external scripts called “external1.bat” and “external2.bat”, with “mainscript.bat” having the following lines:

@echo off

When you try to run the script, you’ll notice that external1.bat will run but external2.bat won’t. This is because external1.bat (the called script) won’t pass the control back to mainscript.bat (the calling script) — which means it won’t get to run external2.bat.

The “CALL” Command

In order to pass the control back to the calling script, you need to use the “CALL” command.

Here’s a simple fix for mainscript.bat:

@echo off
CALL c:testscriptsexternal1.bat
CALL c:testscriptsexternal2.bat

When you run mainscript.bat,  it will call (run) external1.bat. When external1.bat finishes, it will pass the control back to mainscript.bat. Which will then process the next line on the script — in this case, it will call (run) external2.bat — and so on.

Posted in Technology and tagged , , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *