1

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

  • Are the EXE files command-line utilities, or GUI applications? – AFH Jun 17 '19 at 15:21
  • exe files are compiled from matlab and i have no access to the source code – fdiogo Jun 17 '19 at 15:24
  • Do the completion messages come out after the scripts have finished, but with the wrong time? – AFH Jun 17 '19 at 15:39
  • Nothing comes out of the script, it just creates a file. I want the execution time to come from echo ended at !time:~0,5! – fdiogo Jun 17 '19 at 15:59
  • You say the scripts run for ten minutes, so do the end messages come out immediately or ten minutes later? – AFH Jun 17 '19 at 16:05
1

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.
  • Thanks, that does work! It is still I bit more clumbersome than I would have liked, I was hoping there would be a one-liner, but I'll take it :) – fdiogo Jun 18 '19 at 13:55
  • I had hoped that launcher.cmd could be incorporated as a subroutine within scheduler.cmd, but sadly you can't use start /b :launcher ..., so I was left with my inelegant solution (very frustrating to someone used to bash). – AFH Jun 18 '19 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.