0

I get a log file at the end of my build which indicates the status of the projects I build, I want to use this log file to give me exit code 1 when failed builds are not equal to 0.

Log file:

========== Build: 19 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

How do I create a batch file that sees if there are 1 failed(or more), and set %ERRORLEVEL%=1.

0

2 Answers 2

2

Not tested:

set "log=log.txt"

for /f "tokens=5 delims= " %%# in ('find "========== Build:" "%log%" ') do set /a fails=%%#

if %fails% neq 0 exit /b 1
Sign up to request clarification or add additional context in comments.

1 Comment

It might be nice to capture the failure count, but the OP did not ask for it. If a simple binary Success or Failure detection is the goal, then only a single FINDSTR is all that is needed.
2

If all you want to do is set ERRORLEVEL to 0 upon success, and 1 if there was at least one failure, then a simple FINDSTR is all that is needed.

findstr /rc:"^========== Build: .* succeeded, 0 failed," "log.txt" >nul

ERRORLEVEL will be 0 if the string was found, 1 if not.

You can conditionally take action based on success or failure by using the && and || operators

findstr /rc:"^========== Build: .* succeeded, 0 failed," "log.txt" >nul && (
  rem Success actions go here
) || (
  rem Failure actions go here
)

So if you want to simply exit with an ERRORLEVEL of 1 if there was a failure, you could use:

findstr /rc:"^========== Build: .* succeeded, 0 failed," "log.txt" >nul || exit /b 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.