Findstr Returns No Errorlevel When Checking Build Log

Jenkins is calling a batch script. This batch script builds a .NET project with msbuild, which is logged to a logfile.

Now I want to check if the build has failed. Part of this is done with these sort of commands:

findstr /N /C:"Build FAILED." %LOGFILE%
set "FINDSTRERR=%ERRORLEVEL%"
echo "FINDSTRERR: %FINDSTRERR%"
if "%FINDSTRERR%"=="0" (
    echo "ERROR: Build failed."
    exit 1
)

So the batch script searches the logfile for the string Build FAILED and if it is there, the batch script should exit with exit code 1.

When I look to the output of these files, I see this:

6164:Build FAILED.
"FINDSTRERR: "

and script is running futher. But I am expecting something like "FINDSTRERR: 0".

Why batch / findstr is not working here as expected?

1

1 Answer

FINDSTR will set %ERRORLEVEL% as follows:


0 (False) a match is found in at least one line of at least one file.

1 (True) if a match is not found in any line of any file, (or if the file is not found at all).

2 Wrong syntax

An invalid switch will only print an error message in error stream.


  • But I prefer not to do that with if | else errorlevel [0-2], let me suggest that you try to manipulate the output command using && operators (do this) |or| (do this), when doing this mechanical process, you will do the same as errorlevel (in an if else condition); in addition, it will not be necessary to create a variable (%FINDSTRERR%) and applying its value update.

By doing this jobs with the operators, you can create your own errorlevel 0 | 1 from operators && (1)||(0) according to your behavior:


@echo off && color 0a && setlocal enabledelayedexpansion & title ...\%~nx0

set "LOGFILE=D:\path\to\folder\where\are\the\file_log\target.log" && echo[

"%__APPDIR__%\findstr.exe" /N /L /C:"Build FAILED\." "!LOGFILE!" >nul && (
echo/ [1] ERROR: Build Failed^^!! ) || (echo/ [0] Build: Successfull^^!! )

"%__APPDIR__%timeout.exe" /t 2 >nul & endlocal && goto :EOF


  • Output case && Build Failed. string match in file.log:
[1] ERROR: Build failed!
 ^---:> == your errorlevel

  • Output case || Build: Successfull!. string not match in file.log:
[0] Build: Successfull!
 ^---:> == your errorlevel

Obs.: - Edit: set "LOGFILE=D:\path\to\folder\where\are\the\file_log\target.log", with full drive:/path/to/file.log...


Sorry my limited English

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest