Here is one solution for this task:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /L %%I in (1,1,15) do start "" program1.exe arg1
rem A real endless running loop is never a good designed loop.
rem For that reason use a second condition to exit the wait loop.
set "MaxSecondsToWait=60"
:WaitLoop
%SystemRoot%\System32\timeout.exe /T 1 /NOBREAK >nul
%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq program1.exe" /NH 2>nul | %SystemRoot%\System32\find.exe /C "program1.exe" >nul
if errorlevel 1 goto NextLoop
set /A MaxSecondsToWait-=1
if not %MaxSecondsToWait% == 0 goto WaitLoop
echo Timeout for all started program1.exe exceeded.
:NextLoop
for /L %%I in (1,1,15) do start "" program2.exe arg1
endlocal
tasklist
always exits with 0
even on executable to find in list of running tasks cannot be found by tasklist
. For that reason the filter output of tasklist
is filtered by find
which exits with 1
if the case-sensitive searched string is not found in output of tasklist
which means there is no program1.exe
running anymore.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
find /?
for /?
goto /?
set /?
setlocal /?
start /?
tasklist /?
timeout /?
See also Microsoft article about Using command redirection operators for an explanation of 2>nul
and >nul
and |
.