Search code examples
windowsbatch-filecmdparallel-processing

How to run three batch files in parallel and run another three batch files after finishing first set?


I have around 24 batch files which I have to run 3 at a time and after finishing first 3 then I to have run next 3 files and so on.

Suppose I have files like 1.bat,2.bat, 3.bat and so on I need them to run first 3 upon finishing first 3 files I need next 3 files to run and so on till all 24 files.


Solution

  • start 1.bat
    start 2.bat
    start /wait 3.bat
    start 4.bat
    start 5.bat
    start /wait 6.bat
    

    And so on. This assumes that the batch-file with the /wait switch is the one to take longest. If that is not possible you can use this script here:

    @echo off
    
    start bat1.bat
    start bat2.bat
    start bat3.bat
    call waitForFinish
    start bat4.bat
    start bat5.bat
    start bat6.bat
    call waitForFinish
    Goto:eof
    
    :waitForFinish
    set counter=0
    for /f %%i in ('tasklist /NH /FI "Imagename eq cmd.exe') do set /a counter=counter+1
    if %counter% GEQ 2 Goto waitForFinish
    

    After starting 3 batch-files you call the "function" waitForFinish. This checks whether it finds more than 1 running command-line-process (one is used for the running batch-file so it will always be present and one additional line is above the output) and counts up for each window it finds.
    If that counter is greater than 2 it will do the same again and again up to the point where only the running batch-file window is the only cmd.exe process found. If that is the case, the script returns to the top part of the script to start the next three.