I am trying to get a batch file to run a program and save the output with a prepended DateTime on each line to a log file, which needs to update in real time. We are restricted as we can only use cmd, powershell is not possible.
I have tried using:
(for /f "delims=" %%i in ('ConsoleApp2.exe') do echo %DATE% %TIME% %%i >> log.txt)
but this only evaluates after the program has finished running and also gives all the output the same value.
I can't work out how to do this, can anyone please figure this out?
Edit:
Previously we used:
ConsoleApp2.exe >> log.txt
Which output to the log file as the program ran, however I couldn't work out how to get this to append the timestamp.
From comments (thank you Stephan):
setlocal enabledelayedexpansion
(for /f "delims=" %%i in ('ConsoleApp2.exe') do echo !TIME! %%i >> log.txt)
Fixes the issue of getting the timestamp to update correctly, however I still am unable to get an output in real time (while the program is running).
Edit: Lit's answer works however I really need a cmd (not being run in or running Powershell) only answer. An answer explaining how and why this is not possible would also be accepted.