EricBlair1984
EricBlair1984

Reputation: 369

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?

Upvotes: 1

Views: 1234

Answers (1)

Stephan
Stephan

Reputation: 56155

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

Upvotes: 2

Related Questions