It looks like it only matters if the log file contains the string Failed: X
with X
being a number greater 0
. For that reason the FOR loop is not necessary at all. It is enough to search entire file for such a string, perhaps in right context like there must be Total tests:
at beginning of the line to avoid false positives, and just evaluate next if the string is found in the log file or not.
What happens on running the following command line as in code in question?
findstr /i "Failed: 0." Automation_Log_20191125_1853_06.log
There is only specified the option /i
to do a case-insensitive search.
There is neither used option /L
to explicitly request a literal search nor the option /R
to explicitly request a regular expression search. The search string is specified with just putting it in double quotes. In this case FINDSTR analyzes the search string and determines itself if using a literal or a regular expression search.
A search string specified in double quotes containing a space character is interpreted as regular expression string with interpreting the space as OR expression.
The execution of findstr /?
in a command prompt window outputs the help of command FINSTR describing the regular expression characters and their meanings.
The usage of /C:"Failed: 0"
would result by default in a literal search (/L
is used implicit on not being specified explicit) with interpreting the space character as literal space and not as OR expression. That is the most important difference between "search string"
and /C:"search string"
. The space in /C:"search string"
is always interpreted as literal character, even on using additionally /R
to run a regular expression search with the search string in double quotes after /C:
. The space in argument "search string"
without /C:
is interpreted as space only on using additionally option /L
. The space is interpreted as OR expression on using "search string"
with /R
or without /L
.
Conclusion:
findstr "word1 word2 word3"
findstr /R "word1 word2 word3"
FINDSTR is searching with a regular expression for lines containing word1
OR word2
OR word3
.
findstr /C:"word 1" /C:"word 2" /C:"word 3"
findstr /L /C:"word 1" /C:"word 2" /C:"word 3"
FINDSTR is searching with a literal search for lines containing word 1
OR word 2
OR word 3
with space being interpreted as space.
It is advisable to always use /L
or /R
to make it 100% clear for FINDSTR and for readers of the command line if "search string"
or /C:"search string"
is interpreted as literal search string or as regular expression search string.
So this command line results in searching for lines containing case-insensitive either the string Failed:
OR the string 0
. Such this search is definitely not wanted here as it will always finds the line with Failed:
independent on the number.
The value of FINDSTR returned to calling process on exit is 0
on at least one line found matching the search criteria (search successful) or 1
on no line found matching the search criteria (search failed). The exit code of FINDSTR is assigned by cmd.exe
calling findstr.exe
to ERRORLEVEL
.
So the following code could be used to search case-sensitive for a line starting with Total tests:
and containing also Failed:
with next character being a digit greater 0
and evaluate the exit code for success on finding a line matching the search criteria.
%SystemRoot%\System32\findstr.exe /R /C:"^Total tests: .*Failed: [123456789]" Automation_Log_20191125_1853_06.log >nul
if not errorlevel 1 echo Send the log file Automation_Log_20191125_1853_06.log with an email.
A search string specified with /C:
is by default interpreted as literal search string, but /R
overrides that and so the search string is interpreted as regular expression string, but with space being interpreted as space character and not as OR expression as it would be the case on omitting /C:
. ^
means beginning of line and .*
means any character 0 or more times and [123456789]
means one of the characters inside the square brackets.
The lines output by FINDSTR on a positive match are suppressed by redirecting them to device NUL as those lines are not really needed.
It is only of interest if FINDSTR exited with a value less than 1 which means with value 0 because that means the log file contains a line reporting at least one failed operation.
See also the help output on running if /?
and read: