0

I'm using a batch script to invoke the MSVC compiler. I'm trying to handle the output from the compiler line-by-line as it appears (as opposed to waiting for the compiler to finish and handling all the output at once, as for /F would do). Additionally, I need to know the exit code returned by the compiler.

For the first part, I've found that piping to another batch script works.

(%COMPILER_COMMAND%)|Parser.bat
if errorlevel 1 goto Fail

However, the errorlevel check here is looking at the Parser exit code. The Parser doesn't know the exit code of the compiler so it can't return anything meaningful.

If I try to 'forward' the compilers success/failure the errorlevel check ends up checking the exit code of echo. Again, not very helpful.

(%COMPILER_COMMAND% && echo Success || echo Failure)|Parser.bat
if errorlevel 1 goto Fail

I even tried setting a variable as part of the command but it comes out empty.

(%CC% %SRCPATH% %LN% & set error=!ERRORLEVEL!)|Parser.bat
echo Error %error%

How can I handle output from a command line-by-line and still be able to retrieve the errorlevel?

Adam
  • 1,122
  • 9
  • 21
  • I don't think it will work with a pipe. If the first command returns an error level and you print that it works but even if you print it as the first thing in the piped batch file it is 0 - as if the piping clears the error level. Perhaps the thing to do is to use popen to execute the compiler and read the output because then you might be able to get the error level as well. – Jerry Jeremiah Jan 23 '18 at 22:17
  • Although here is a question with answers: https://stackoverflow.com/questions/877639/how-do-i-get-the-error-level-of-commands-in-a-pipe-in-windows-batch-programming And there is this: https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/a4VqFgepnvw – Jerry Jeremiah Jan 23 '18 at 22:18

0 Answers0