Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I want it to execute in an if statement.

like image 534
Kev Avatar asked Jan 25 '11 21:01

Kev


People also ask

How do you call a batch file from another batch file with parameters?

@ppumkin: You can call my batch file from another and pass the parameters on the call line. Your approach seems to be to call the batch file from another and pass the parameters by setting environment variables. This is also a valid approach, mine is just an alternative I like to use and propose.


2 Answers

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.

like image 181
yhw42 Avatar answered Sep 29 '22 13:09

yhw42


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! 
like image 38
Dan Fabulich Avatar answered Sep 29 '22 14:09

Dan Fabulich