How do I run a batch script from within a batch script?

Command LineBatch FileCmd

Command Line Problem Overview


How do I call another batch script from within a batch script?

I want it to execute in an if statement.

Command Line Solutions


Solution 1 - Command Line

Use CALL as in

CALL nameOfOtherFile.bat

This will block (pause) the execution of the current batch file, and it will wait until the CALLed one completes.

If you don't want it to block, use START instead.

Get the nitty-gritty details by using CALL /? or START /? from the cmd prompt.

Solution 2 - Command Line

You can just invoke the batch script by name, as if you're running on the command line.

So, suppose you have a file bar.bat that says echo This is bar.bat! and you want to call it from a file foo.bat, you can write this in foo.bat:

if "%1"=="blah" bar

Run foo blah from the command line, and you'll see:

C:\>foo blah

C:\>if "blah" == "blah" bar

C:\>echo This is bar.bat!
This is bar.bat!

But beware: When you invoke a batch script from another batch script, the original batch script will stop running. If you want to run the secondary batch script and then return to the previous batch script, you'll have to use the call command. For example:

if "%1"=="blah" call bar
echo That's all for foo.bat!

If you run foo blah on that, you'd see:

C:\>foo blah

C:\>if "blah" == "blah" call bar

C:\>echo This is bar.bat!
This is bar.bat!

C:\>echo That's all for foo.bat!
That's all for foo.bat!

Solution 3 - Command Line

You should use CALL

CALL batch.bat

Solution 4 - Command Line

You can use

call script.bat

or just

script.bat

Solution 5 - Command Line

If you wish to open the batch file in another window, use start. This way, you can basically run two scripts at the same time. In other words, you don't have to wait for the script you just called to finish. All examples below work:

start batch.bat
start call batch.bat
start cmd /c batch.bat

If you want to wait for the script to finish, try start /w call batch.bat, but the batch.bat has to end with exit.

Solution 6 - Command Line

Here is example:

You have a.bat:

@echo off
if exist b.bat goto RUNB
goto END
:RUNB
b.bat
:END

and b.bat called conditionally from a.bat:

@echo off 
echo "This is b.bat"

Solution 7 - Command Line

to run both the batch files better use

       start Call "batch_file_name.bat"

if you just say

       start"batch_file_name.bat"

sometimes it only opens a "cmd window" with just prompt,and you'll see the code is not getting executed.

Solution 8 - Command Line

huh, I don't know why, but call didn't do the trick
call script.bat didn't return to the original console.
cmd /k script.bat did return to the original console.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevView Question on Stackoverflow
Solution 1 - Command Lineyhw42View Answer on Stackoverflow
Solution 2 - Command LineDan FabulichView Answer on Stackoverflow
Solution 3 - Command LineYounes TARCHOUNView Answer on Stackoverflow
Solution 4 - Command LineIlya SaunkinView Answer on Stackoverflow
Solution 5 - Command LineGChufView Answer on Stackoverflow
Solution 6 - Command LinestanikView Answer on Stackoverflow
Solution 7 - Command LineShekar BashaView Answer on Stackoverflow
Solution 8 - Command LineStevenView Answer on Stackoverflow