Wait For Commands to Finish

13,886

Solution 1

You may use several flag files, one per running Batch file. For example:

First batch file create Flagfile.1 when enter, and delete it before end, and the same way the rest of concurrent batch files (Flagfile.2,...)

The main file just must wait for all flagfiles disappear. For example, in Main file:

start Batch1
start Batch2
.....
:wait
if exist flagfile.* goto wait

In any Batch file, for example Batch1:

echo %time% > flagfile.1
echo Do my process...
del flagfile.1
exit

Solution 2

My suggestion is to use CALL command. An example from Batch Files:

Batch1.bat listing:

REM Batch1.bat
SET ABC=1
CALL BATCH2.BAT %ABC%
ECHO ABC = %ABC%
BATCH2.BAT %ABC%
ECHO ABC = %ABC%

where Batch2.bat listing is:

REM Batch2.bat
SET ABC=%ABC%%1


EDIT: based on feedback from @Andriy M, here is an improved version for the two batches:

Batch 1:

@ECHO OFF
REM Batch1.bat
SET ABC=1
CALL batch2.bat %ABC%
ECHO 1. ABC = %ABC%
CALL batch2.bat %ABC% REM test this line with CALL and without
ECHO 2. ABC = %ABC%

Batch 2:

@ECHO OFF
REM Batch2.bat
SET tmout=5
echo sleeping %tmout% seconds...
REM this introduces a timeout by using a nonexistent ip address
PING 1.2.1.2 -n 1 -w %tmout%000 > NUL
echo done sleeping
SET ABC=%ABC%%1

See the line in batch1 where I wrote the comment test this line with CALL and without. Run that batch twice, with that line having CALL and without CALL.

The output from console without CALL:

C:\temp>batch1.bat
sleeping 5 seconds...
done sleeping
1. ABC = 11
sleeping 5 seconds...
done sleeping

And now the output from console with CALL:

C:\temp>batch1.bat
sleeping 5 seconds...
done sleeping
1. ABC = 11
sleeping 5 seconds...
done sleeping
2. ABC = 1111

Please note the difference: we get the 2nd echo, q.e.d.

Share:
13,886
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a Windows batch file that starts other batch files. How do I wait for all the other batch files to finish before executing somthing in the first batch? I can't use /wait because I need the other commands to run in parallel.

  • Andriy M
    Andriy M almost 13 years
    That is to say, without CALL it waits just as well as with CALL, but only to… terminate. :)
  • scrat.squirrel
    scrat.squirrel almost 13 years
    @Andriy M: Based on what I've experienced so far, that is not so, without CALL - execution returns immediately to the next line, without waiting for the called batch/command to finish (perhaps you should try that with long operations).
  • Andriy M
    Andriy M almost 13 years
    That's so peculiar that I had to verify my expectation using your very sample scripts. I ran the first batch from command prompt in my Windows 7 SP1 and the two scripts worked for me just as I thought they would, i.e. the execution terminated after batch2.bat terminated when called the second time. That is, the last line of batch1.bat didn't execute. What have you tested them on that you are saying that without CALL the execution returns to the first batch, and without waiting too?
  • scrat.squirrel
    scrat.squirrel almost 13 years
    If you insert a delay in "batch2.bat" you will see the difference when you use CALL or not in the first batch. I will try to post the code here, or edit my answer to include that.
  • Andriy M
    Andriy M almost 13 years
    Ah, it's all right then. You have proved that without CALL the execution stops immediately after batch2.bat is done running the second time, and with CALL the control returns to the batch1.bat after each batch2 call. And in both cases the first batch waits for the second one to complete (well, again, it worked that way for me). Everything is just like I was saying. I'm starting to think our disagreement was based on some misunderstanding.
  • scrat.squirrel
    scrat.squirrel almost 13 years
    @Andriy M: Yeah, I agree, you have a point about waiting, but I wanted to demo something which is useable in practice :)
  • Aacini
    Aacini almost 13 years
    If a Batch file run another one without CALL, the net effect is equivalent to a "goto to another file": when the second file ends, the whole execution ends. I called "Overlay" a Batch file executed this way.
  • Aacini
    Aacini over 12 years
    @woohoo: Excuse me, I think there is a confussion here. The question is about how to wait for all the children parallel Batch'es to end before continuing the parent one. There is no way to solve this problem via CALL, but via START. As the asker said, he can't use /WAIT, that is an option of START command. The solution is to implement a semaphore system that allows the parent Batch file to know when all its children ends. I proposed a very simple semaphore system based on the presence of a file, that, however, should works perfectly. So, why my answer received a downvote? :-(
  • dbenham
    dbenham almost 12 years
    -1, This "solution" completely disregards the OP's desire to have one batch script launch multiple child scripts in parallel, and wait for them to finish.
  • dbenham
    dbenham almost 12 years
    +1, Why the down vote indeed, especially since this is the only answer that actually addresses the question. Better to have the main file create the flagfile. The main script might begin testing before the child processes have finished initializing.
  • Code Maverick
    Code Maverick over 9 years
    LOVE this! In VS, we have been using CALL in each service project's post-build event to download each one's *.cs reference file to store in another project that contains all for one simple assembly reference. I just ran across START /b /i and found I could running them all at the same time, same window, not affecting each other. So sweet. Then, I was like how the heck can I use MSBuild after they all complete to build that service project so the projects that reference its assembly are automatically updated. BOOM!