Using the findstr command in a CMD file and expect ERRORLEVEL to be set. But it is not

140 Views Asked by At

This line works:

findstr /I /C:"Archiving file" "..\LogFiles\%NewLogFileName%"

Outputting the following to the screen:

2023-08-21 13:25:11.9024|Info|Archiving file

The next line I am looking for ERROR in a file. I know that ERROR is NOT in the file, so my expectation is that the error level should be 1, but it is continually 0.

findstr /I /C:"ERROR" "..\LogFiles\%NewLogFileName%"

IF ERRORLEVEL 1 (
     ECHO Error level AFTER Search %ERRORLEVEL%
)

What am I doing wrong?


Since this thread opened I have changed the code to:

findstr /I /C:"ERROR" "..\LogFiles\%NewLogFileName%"

call echo %%errorlevel%% & REM WORKS "1"

REM multiple iteration of these lines fail

REM Some things I have tried 
echo %errorlevel% & REM FAILS "0"

SET "somevar=%%errorlevel%%"

echo %somevar% & REM FAILS "0"

The text files are basic text. one has Archiving file in the contents the other DOES NOT HAVE error in the text.

What more can I provide?

2

There are 2 best solutions below

0
Io-oI On
findstr /ic:"error" "..\LogFiles\%NewLogFileName%" >nul && echo/0 || echo/1 

If your path/file exists or doesn't exist, if the string was found or not, forget errorlevel, use operators && return 0 and || return non 0.

Findstr /flag(s) <file.eXt && (do BC Return 0) || (do BC Return non 0)

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;

@echo off 

command 1
command 2
command ...
command n
 
findstr /ic:"strings xy" <"..\Log\%New%" >nul && (
     command 1
     command 2
     command ...
     command n
     echo/0 
    ) || (
     command 1
     command 2
     command ...
     command n
     echo/1 
    )

command 1
command 2
command ...
command n


Additional resources:

0
Magoo On

As the batch langiage has evolved, the original line-by-line nature has been retained, but the concept of a "line" has changed to that of a "logical line".

Lines may now contain multiple commands separated by &. This has been extended to allow conditional execution by && and ||

Lines may be extended over multiple physical lines by using the line-continuation symbol ^ or by grouping commands within parentheses (know on SO as a block)

Since we sometimes need to use a character that is used as part of the language as a literal, we can "escape" the character (turn off the special meaning of the following character) by preceding it with a caret ^ - but the escape character for % is another %.

Quite how a logical line is executed is covered in this short article

Hence, when a logical line is parsed, any %var% found on the line is replaced by the value of that variable at that time - the time the line is parsed, before execution commences.

So the fragment call echo %%errorlevel%% is interpreted as call echo {escaped-%}errorlevel{escaped-%} and is executed in a sub-process as echo %errorlevel%.

Another approach is the use of delayed expansion