124

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

I want it to execute in an if statement.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kev
  • 1,251
  • 2
  • 8
  • 4

8 Answers8

265

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yhw42
  • 3,334
  • 2
  • 27
  • 24
28

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!
CodeFox
  • 3,321
  • 1
  • 29
  • 41
Dan Fabulich
  • 37,506
  • 41
  • 139
  • 175
11

You should use CALL

CALL batch.bat
5

You can use

call script.bat

or just

script.bat
Conner
  • 30,144
  • 8
  • 52
  • 73
Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
  • 9
    If you directly call another script within your script, in case any command in the called script returns a non-zero value (error), the callee script will stop to execute as well, where using `call`, it will continue its execution even with an error in the called script. – Bruno Finger Oct 24 '17 at 08:27
5

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.

GChuf
  • 1,135
  • 1
  • 17
  • 28
2

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"
stanik
  • 75
  • 6
1

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.

-1

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.

Steven
  • 76
  • 1
  • 6
  • 2
    Consider editing your question to more formally explain what didn't work and what did, including the non working code and working code – artemis Apr 25 '19 at 14:28
  • Naïve comment against the ONLY working solution on the page...what else is to explain besides "didn't return to the original console"....so, cmd/k worked for me, too, after I wasted my time with the other "solutions" – nenea Jan 21 '22 at 13:28