Etienne
Etienne

Reputation: 7201

Find 2 values in file with command-line | CMD

This code below allows me to find the word "error" in all my files using command-line (CMD).

find /c "error" C:\MyFiles\*.txt

But I want it to look for the word "error" and "warning" at the same time.

So I want to put these 2 lines in as 1 line of command.

find /c "error" C:\MyFiles\*.txt
find /c "warning" C:\MyFiles\*.txt

I do not want to see 2 result sets.

Upvotes: 3

Views: 24678

Answers (2)

dbenham
dbenham

Reputation: 130829

This should work (FINDSTR splits the search string at spaces, so it looks for either word).

findstr "error warning" C:\MyFiles\*.txt

As should this equivalent search:

findstr /c:"error" /c:"warning" C:\MyFiles\*.txt

However, there is this bug: Why doesn't this FINDSTR example with multiple literal search strings find a match?. I'm not sure if the above searches would meet the criteria that specify when the bug might affect the results. (I'm not sure if there enough overlap between the search strings.) But better to be safe than sorry.

You can eliminate the possibility of the bug by either making the search case insensitive (not sure if that meets your requirements or not):

findstr /i "error warning" C:\MyFiles\*.txt

or you can convert your search strings into regular expressions. This is trivial to implement in your case since there are no regex meta-characters that need escaping in your search strings.

findstr /r "error warning" C:\MyFiles\*.txt

Upvotes: 8

jeb
jeb

Reputation: 82307

In your case you could simple use a pipe for realize the AND operator.

find /c "error" C:\MyFiles\*.txt | find /c "warning"

From your comment, you need an OR operator

In this case I would do the search with findstr and the counting with find /c

findstr "error warning" C:\MyFiles\*.txt | find /c /v ""

Upvotes: 3

Related Questions