Shahar Hamuzim Rajuan
Shahar Hamuzim Rajuan

Reputation: 6129

Using batch file give exit code according to log file

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.

Upvotes: 0

Views: 123

Answers (2)

dbenham
dbenham

Reputation: 130819

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

Upvotes: 2

npocmaka
npocmaka

Reputation: 57252

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

Upvotes: 2

Related Questions