I have two scripts, foo and bar. Foo calls bar. I need to print the lines of bar's output with pre-pended time, and capture the %ERRORLEVEL% from bar. I can't seem to figure out how to do both. I further can't figure out how to accurately display the time bar's lines were executed, only the time bar finished executing and transferred control back to foo.
foo.cmd
echo off
for /f "delims=" %%a in ('bar') do (call :log "%%a")
echo after loop error level: %ERRORLEVEL%
goto :eof
:log
echo during loop error level: %ERRORLEVEL%
echo [%time%]: %~1
goto :eof
bar.cmd
echo this is log output line 1
timeout /t 2 /nobreak > NUL
echo this is log output line 2
EXIT /B 3
My output from this run is:
>foo
>echo off
during loop error level: 0
during loop error level: 0
[13:24:22.21]: this is log output line 1
during loop error level: 0
during loop error level: 0
during loop error level: 0
[13:24:22.23]: this is log output line 2
during loop error level: 0
after loop error level: 0
My desired from this run is:
>foo
>echo off
during loop error level: 0
during loop error level: 0
[13:24:20.21]: this is log output line 1
during loop error level: 0
during loop error level: 0
during loop error level: 0
[13:24:22.23]: this is log output line 2 <------- this is 2 seconds later
during loop error level: 0
after loop error level: 3 <------ I got %ERRORLEVEL%
Any help is appreciated!