I have a batch file that downloads 21 separate files (all very large). I have timeouts to try to allow some files to finish before trying to get more (not a nice solution), and if they all run together it uses all my system resources and everything grinds to a halt.

I can put /WAIT in my FOR loop, then it will only launch one download task at a time. I would like to launch 5 download windows, and once they are finished, continue with the next 5. I've tried to use GOTO to wait until finished but it does not work.

```
rem *****Step 1*****
set tstart=0
set tend=5
set incr=1
FOR /L %%a IN (%tstart%,%incr%,%tend%) DO (start /ABOVENORMAL GET_PIECE_ENSEMBLE.bat %newformatdate% 0%%a %HH%)
timeout /t 30

:WAIT
if exist flagfile.* goto WAIT

```

Then I have 4 more steps to download files 5-10, 11-16, etc. But if it takes extra long to download, the script moves along and starts downloading more files or moves on to the next processes, which require all the data to be downloaded first. I am trying 2 things here: a short timeout to ensure the data has started downloading (so a flagfile exists), and a wait if flagfile exists. But the wait isn't working, the script still moves along and starts downloading the next bunch. My workaround has been to set a timeout of 600 but sometimes the data takes more than 10 min to download, sometimes it hangs and takes up to two hours, sometimes only a few minutes. If all the pieces launch at once, it freezes up the workstation and we have to restart the machine.

And here is the code of GET\_PIECE\_ENSEMBLE, the script that runs in the FOR loop:

```
set "newformatdate=%1"
set "ename=%2"
set "HH=%3"
echo %time% > flagfile.%ename%
wget -O C:/Data/DataFile_%HH%.%ename%.grb2 http://url/%newformatdate%.grib2
del flagfile.%ename%

```

Looking for advice on either how to make the "wait if flagfile exists" work (any idea why this isn't working?), or if there is some simple argument I can put in my FOR loop to limit the number of instances so downloads finish before others start? Surely there is a better solution than guessing at a timeout interval when the network conditions vary so much!