0
::loop1
For /L %%A IN (1,1,15) DO (
start program1.exe  arg1 
)

::loop2
For /L %%A IN (1,1,15) DO (
start program2.exe  arg1 
)

I want to run loop2 only when all 15 instances or program1.exe from loop 1 have finished executing. I cannot use call since i want the programs to start in parallel.

Bounty Collector
  • 615
  • 7
  • 19
  • [tasklist](https://ss64.com/nt/tasklist.html) should be helpful. – Stephan Mar 05 '20 at 17:53
  • This can be done by surrounding the `FOR` command inside another paranthesized code block and piping it to the `SET /P` command. Been a while since I have done it. I have to find the exact code. I did provide it as answer on SO in the past because I learned it from another user on SO. I think it was @Aacini. – Squashman Mar 05 '20 at 18:12
  • Does this answer your question? [How to wait all batch files to finish before exiting?](https://stackoverflow.com/questions/33584587/how-to-wait-all-batch-files-to-finish-before-exiting) – Squashman Mar 05 '20 at 18:14
  • I will test it soon, thanks. didnt know about the set /p trick. – Bounty Collector Mar 05 '20 at 18:17

1 Answers1

1

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 |.

Mofi
  • 46,139
  • 17
  • 80
  • 143