tags:

views:

730

answers:

2

I have a batch file,

bat1.bat
bat2.bat

but it stops at the end of bat1

any clues?

+3  A: 

Use call:

call bat1.cmd
call bat2.cmd

By default, when you just run a batch file from another one controll will not pass back to the calling one. That's why you need to use call.

Basically, if you have a batch like this:

@echo off
echo Foo
batch2.cmd
echo Bar

then it will only output

Foo

If you write it like

@echo off
echo Foo
call batch2.cmd
echo Bar

however, it will output

Foo
Bar

because after batch2 terminates, program control is passed back to your original batch file.

Joey
second example identical to first, but I got the point. Works perfectly
Nick
I think the second example is missing a "call."
Gary van der Merwe
Thanks Gary. Damn those copy and paste errors. Fixed.
Joey
A: 

This can happen if bat1.bat stops abnormally (other than just running to the end, like calling exit) and you can work around this by using a fresh cmd.exe to run each bat file:

start /b /wait bat1.bat
start /b /wait bat2.bat

You could omit it for the last one if there won't follow commands in you bat file.

x4u
No, it also happens if the batch terminates normally. Using `start` here is overkill, though. And you need an extra `exit` at the end of the sub-batches to kill the `cmd` process that is spawned. Otherwise you find yourself on a new console after the first batch ran.
Joey