Getting start and end timing for processes running in parallel

I have the following batch script (slightly simplified) to run a series of exe files in parallel.

SETLOCAL ENABLEDELAYEDEXPANSION

for %%a in (1 2 4 8) do ( 
  set i=%%a
  set script=calculate_V!i!.exe
  start echo started V!i! at !time:~0,5! ^>^> log.txt ^2^>^&^1 
 ^& !script! ^& echo ended V!i! at !time:~0,5! ^>^> log.txt ^2^>^&^1 ^& exit
)

I want to get the start and end time of the script's run, but the problem is that the delayed expansion of the second time (echo ended V!i! at !time:~0,5!) is made simultaneously with the first, so the output is (for example)

started V1 at 15:50
started V2 at 15:50
...
ended V1 at 15:50
ended V2 at 15:50
...

even though the script took 10 minutes to run.

How can I evaluate !time:~0,5! only after script runs?

Thanks


I have looked at this and the only way I can get it to work is to make sure that the time look-ups are on separate lines, and I have not managed to do this without using two batch files.

I have used timeout /t as a substitute for running a task which takes a specific time.

launcher.cmd:-

@echo off
echo.
echo %time% start %1 >>logfile.txt
timeout /t %1 2>&1 1>nul:
echo %time% end %1 >>logfile.txt
exit

scheduler.cmd:-

@echo off
start /b launcher 5
start /b launcher 10

logfile.txt:-

20:24:01.15 start 5
20:24:01.22 start 10
20:24:06.20 end 5
20:24:11.19 end 10

It should be straightforward to adapt these files for your purposes.

Notes:-

  • start /b stops multiple cmd windows from opening; alternatively, start /min will use separate windows, but without too much visual intrusion.
  • Because time is a volatile variable, it does not need to use delayed expansion.
  • If the scheduled task is a Windows (not a command-line) program, launcher will need to use start /wait to run it.
  • The exit makes sure that the launcher threads terminate.