::+-------------------------------------------------------------------+
::| TEST.BTM - Show how to run several batch files at the same time, |
::| and determine when they have all finished. |
::| |
::| February 29, 2012 - Joe Caverly |
::|-------------------------------------------------------------------+
@setlocal
@echo off
::
:: Create five batch files
::
echo delay 7 > proc1.btm
echo delay 2 > proc2.btm
echo delay 3 > proc3.btm
echo delay 4 > proc4.btm
echo delay 5 > proc5.btm
::
:: Track the processes with a counter
::
set procs_running=5
::
:: Start each batch file in detached mode
::
detach proc1.btm
set proc1=%_detachpid
detach proc2.btm
set proc2=%_detachpid
detach proc3.btm
set proc3=%_detachpid
detach proc4.btm
set proc4=%_detachpid
detach proc5.btm
set proc5=%_detachpid
::
:: Check the progress of the detached batch files
::
do until %procs_running lt 1
iff defined proc1 then
iff %@isproc[%proc1] eq 0 then
echo Process 1 finished.
unset proc1
set procs_running=%@dec[%procs_running]
endiff
endiff
iff defined proc2 then
iff %@isproc[%proc2] eq 0 then
echo Process 2 finished.
unset proc2
set procs_running=%@dec[%procs_running]
endiff
endiff
iff defined proc3 then
iff %@isproc[%proc3] eq 0 then
echo Process 3 finished.
unset proc3
set procs_running=%@dec[%procs_running]
endiff
endiff
iff defined proc4 then
iff %@isproc[%proc4] eq 0 then
echo Process 4 finished.
unset proc4
set procs_running=%@dec[%procs_running]
endiff
endiff
iff defined proc5 then
iff %@isproc[%proc5] eq 0 then
echo Process 5 finished.
unset proc5
set procs_running=%@dec[%procs_running]
endiff
endiff
enddo
::
:: All detached batch files have finished
::
::
:: Remove the batch files created earlier
::
if exist proc1.btm del proc1.btm > nul
if exist proc2.btm del proc2.btm > nul
if exist proc3.btm del proc3.btm > nul
if exist proc4.btm del proc4.btm > nul
if exist proc5.btm del proc5.btm > nul
endlocal