The Windows FINDSTR command is horribly documented. There is very basic command line help available through FINDSTR /?
, or HELP FINDSTR
, but it is
The findstr command sets the ErrorLevel (or exit code) to one of the following values, given that there are no invalid or incompatible switches and no search string exceeds the applicable length limit:
0
when at least a single match is encountered in one line throughout all specified files;1
otherwise;A line is considered to contain a match when:
/V
option is given and the search expression occurs at least once;/V
option is given and the search expression does not occur;This means that the /V
option also changes the returned ErrorLevel
, but it does not just revert it!
For example, when you have got a file test.txt
with two lines, one of which contains the string text
but the other one does not, both findstr "text" "test.txt"
and findstr /V "text" "test.txt"
return an ErrorLevel
of 0
.
Basically you can say: if findstr
returns at least a line, ErrorLevel
is set to 0
, else to 1
.
Note that the /M
option does not affect the ErrorLevel
value, it just alters the output.
(Just for the sake of completeness: the find command behaves exactly the same way with respect to the /V
option and ErrorLevel
; the /C
option does not affect ErrorLevel
.)