user1634603
user1634603

Reputation: 151

Run batch files sequentially

I want to ask you all how to run batch files sequentially in Windows. I have tried :

start /w batchfile_1.bat 
start /w batchfile_2.bat
..
start /w batchfile_n.bat

but I have to close the previous .bat file process manually (e.g. by clicking) before continuing into the next one. Is there any solution to do this automatically without me doing the manual closing previous .bat program every time?

Thanks a lot.

Upvotes: 15

Views: 90219

Answers (3)

Andriy M
Andriy M

Reputation: 77657

I'm not sure but based on your comments, the following seems to be happening when you run that sequence of START commands:

  1. A START /W command is invoked and starts a batch file.

  2. The batch file starts executing and runs a program.

  3. The batch file finishes and its console window remains open, but the program continues running.

  4. The START /W command that was used to run the batch file is still executing because the console window remains open.

  5. You wait until the program terminates, then you close the console window, and then the next START /W command is invoked, and everything is repeated.

Now, if you place EXIT at the end of every batch file you want to run sequentially, that makes situation worse because it causes the console window to close after the batch script completes, and that in turn ends the corresponding START /W command and causes another one to execute, even though the program invoked by the batch script may still be running. And so the effect is that the batch scripts (or, rather, the programs executed by them) run simultaneously rather than sequentially.

I think, if this can be solved at all, you need to move the START /W command and put it in every batch file in front of (every) command that that batch file executes and doesn't wait for the termination of. That is, if your batchfile_1.bat runs a program.exe, change the command line to START /W program.exe, and similarly for other relevant commands in other batch files.

Upvotes: 0

James K
James K

Reputation: 4055

If you are in love with using START, you could have your batch files end with the EXIT command. That will close the windows created by the start command.

@echo off
 .
 .
:: Inspired coding
 .
 .
exit

Upvotes: 3

Ishikawa
Ishikawa

Reputation: 668

I would check the solutions to this question: Run Multiple batch files

  • Taken from the answer in the link.

Use call:

call bat1.cmd
call bat2.cmd

By default, when you just run a batch file from another one control 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.

Upvotes: 24

Related Questions