Wii
Wii

Reputation: 301

How to call one batch file after another

I have a batch file that i am testing, all i want to do is the following

CALL ping.bat

Then after that batch file has run i want to run another file:

CALL ping2.bat

Right now i have these two lines beside each other in a batch file the first batch file will fire successfully but the second one does not . Any suggestions?

CALL ping.bat
CALL ping2.bat

Ping .bat is just:

ping 127.0.0.1    

Upvotes: 28

Views: 78127

Answers (6)

knkarthick24
knkarthick24

Reputation: 3216

Assume you have 3 batch files.

  1. ping1.bat which has the content ping 127.0.0.1
  2. ping2.bat which has the content ping 127.0.0.1
  3. ping3.bat which has the below two lines
    call ping1.bat
    call ping2.bat

If you have all the three batch files in a single folder,(lets say under C:\NewFolder) then if you double click ping3.bat, you won't get any error for sure.

Note: If you don't want to wait for the first command to complete, then use start keyword which just initiate the process and proceed with the next line in the batch file, whereas call will do it sequentially(comes to next line only after the current process completes , start allows parallelism)
To do it parallel use the below two lines of code in the ping3.bat:

start ping1.bat
start ping2.bat

Upvotes: 5

Derek Nutile
Derek Nutile

Reputation: 211

The ping command acts differently on different operating systems. Try forcing the ping command to stop after a few echo requests with a -n switch.

ping -n 4 127.0.0.1

Upvotes: 0

Goran Vasic
Goran Vasic

Reputation: 1099

Not exactly sure what you wanted to do here, but I assume that you wanted to do this:

  1. run FIRST.bat
  2. from FIRST.bat you wish to call SECOND.bat
  3. While SECOND.bat is executing, FIRST.bat should remain paused
  4. After SECOND.bat finishes its execution, FIRST.bat should resume and call THIRD.bat

In that case, from your actual batch file, you should start ping.bat and ping2.bat like this:

::some code here
start /wait ping.bat
start /wait ping2.bat
::some code here

Then in both ping.bat and ping2.bat the last line should be exit. Their code should look like this:

::some code here, might be ping 127.0.0.1
exit

So now your actual batch file will start ping.bat and it will wait for it to finish (exit). Once the ping.bat closes, your actual batch file will go to the next line and it will start ping2.bat etc.

Upvotes: 2

Andriy M
Andriy M

Reputation: 77657

There's a chance your ping.bat is simply calling itself, if its contents is merely ping 127.0.0.1, as you say.

I would append .exe after ping to make things sure.

As jeb has by all means justly suggested, choosing a different name for your batch file is an even better solution.

Upvotes: 6

Wii
Wii

Reputation: 301

don't call the file you are calling from the batch the same name as the command you are trying to invoke...renamed to gnip.bat and works fine

Upvotes: 2

Joey
Joey

Reputation: 354386

Check that you don't have exit somewhere in the first batch. Some people habitually use that to jump out of a batch file which is not the proper way to exit a batch (exit /b or goto :eof is).

Another option is that you might call another batch in the first one without call.

Upvotes: 18

Related Questions