15

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.

Brad
  • 159,648
  • 54
  • 349
  • 530
user1634603
  • 151
  • 1
  • 1
  • 3
  • Have you tried simply running them without `start /w`? It's been awhile, but they should run in order, wait for each other to finish, and not open in new windows and such requiring user input. – Brad Aug 29 '12 at 23:35
  • I would check the solutions to this question: [Run Multiple batch files](http://stackoverflow.com/questions/2071408/how-to-run-multiple-batch-files-in-serial-in-windows-command-line-environment) – Ishikawa Aug 29 '12 at 23:35
  • @Brad: Yes, without start /w, or only using start, the .bat files will run simultaneously, not sequentially as I wanted. I have also used call, but it also make the multiple .bat files run simultaneously. – user1634603 Aug 30 '12 at 04:38
  • @user1634603, No, try it without using `start` or `call` at all. – Brad Aug 30 '12 at 13:13
  • Foolishly put a close vote without paying more attention to the comments. Sorry. – Andriy M Aug 31 '12 at 08:37

3 Answers3

23

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.

Community
  • 1
  • 1
Ishikawa
  • 658
  • 1
  • 6
  • 14
  • It's easier just to have the first batch file end by executing (not `call`ing) the second one, the second executes the third, and so forth. If you want them to be guaranteed to run in sequence, it's the only way to do it that will do so; if one crashes, it won't call the next and the execution chain will be broken. – Ken White Aug 29 '12 at 23:42
  • 1
    But what happens if the OP (or anyone else) wanted to later change or add the order of the batch files. Would this way not be easier? Where all the batch files are run from one main file? – Ishikawa Aug 29 '12 at 23:44
  • The problem is with `call` there can be an issue with one of the files in the sequence that makes it exit early, and the subsequent files will still be executed when control returns to the "main file". You can't guarantee the sequence executes strictly in order. (Not downvoting your answer, just commenting.) – Ken White Aug 29 '12 at 23:47
  • Ah I see, I am kind of new to this as well so I didn't think about that. I guess this would work if the batch file order is not exactly crucial but just needs to be run at the same time. Thanks for your input. – Ishikawa Aug 29 '12 at 23:48
  • @KenWhite "The problem with `call`..." Not if you use `EXIT /b` and check to see that the errorcode is 0 before running the next file. – James K Aug 30 '12 at 01:53
  • @JamesK: But what if something unexpected happens before you reach `EXIT /b`? If the batch file doesn't reach that line but exits prematurely, the next `call` still executes. If you chain the batch files, that can't happen; the failure stops the next file from being executed. – Ken White Aug 30 '12 at 01:58
  • @KenWhite: But if you set the errorlevel to, say 1000, at exit then if it returns with any other code you know it broke. Like `EXIT /b 1000` (I admit, I was originally thinking that the errorcode was a little more robust than it actually it.) – James K Aug 30 '12 at 02:11
  • @JamesK: Yep, if you jump through a million loops to get there, you can do it the way you suggest; or, you could just call them sequentially as I said and not fight it. – Ken White Aug 30 '12 at 02:13
  • Sometimes you gots to do what you gots to do! ^_^ – James K Aug 30 '12 at 02:15
  • Please Ishikawa, Ken White, or James K, post a COMPLETE, EXACT word for word, character for character, TESTED, VERIFIED, and above all WORKING, example of the .BAT file doing what you say? If these 3 fellows are no more there, could anyone answer? Thanks In Advance! – Michel Merlin May 28 '22 at 20:29
3

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
James K
  • 4,005
  • 4
  • 20
  • 28
  • 1
    BTW, do not use `EXIT /b` with batch files you `START`ed. It does not close windows, and is intended to pass the errorlevel back to the calling file. Which is impossible because you launched a separate cmd.exe environment by using `START`. – James K Aug 30 '12 at 01:58
  • Thanks for the suggestion. However, using exit command indeed close the windows that appeared when I start the .bat, but the process become simultaneously executed, and not sequentially as I wanted to be. – user1634603 Aug 30 '12 at 06:13
0

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.

Andriy M
  • 76,112
  • 17
  • 94
  • 154