Search code examples
windowsbatch-filecmdparallel-processingwait

In Windows batch loop, how to wait for spawned processes to complete before continuing


I want to fire off multiple commands within a loop in a batch file like this:

for /l %%x in (20170101,1,20170105) do (
start /wait C:\Progra~1\Amazon\AWSCLI\aws s3 cp s3://bucket1/%%x 
s3://bucket2/%%x --recursive
)

#do something else here only when ALL the above commands complete

Will the Start /wait have the effect of waiting until all commands complete before moving to next line after the loop?


Solution

  • start /wait isn't "global", it just waits for the started process to finish (maybe... depends on how the application is programmed). What you need (starting several processes in parallel and wait until the last is finished) can be done with a different method: give all your processes a defined title and watch them:

    @echo off
    setlocal enabledelayedexpansion
    for /l %%i in (1,1,10) do (
       start "OneOfMyProcesses" timeout !random:~-1!
    )
    echo waiting
    :loop
    timeout 1 >nul
    tasklist /v |find "OneOfMyProcesses" >nul && goto :loop
    echo all of them are finished
    

    Note: as mentioned above, this may or may not work with your application