What are the undocumented features and limitations of the Windows FINDSTR command?

后端 未结 8 1688
耶瑟儿~
耶瑟儿~ 2020-11-21 04:23

The Windows FINDSTR command is horribly documented. There is very basic command line help available through FINDSTR /?, or HELP FINDSTR, but it is

8条回答
  •  无人共我
    2020-11-21 05:07

    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:

    • no /V option is given and the search expression occurs at least once;
    • the /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.)

提交回复
热议问题