3

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.

Etienne
  • 7,141
  • 42
  • 108
  • 160

2 Answers2

8

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
dbenham
  • 127,446
  • 28
  • 251
  • 390
3

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 ""
jeb
  • 78,592
  • 17
  • 171
  • 225
  • This does work thanks, but if I have 4 "warning" messages and 2 "error" messages in the same file. It only shows a count of the 4. It must show a count of 6 because of the 4 + 2. – Etienne Jun 29 '12 at 07:37
  • The code above only shows lines with both words in. A typical `AND`. But it seems that you need an `OR` solution. – jeb Jun 29 '12 at 07:43
  • Thanks, correct, but I would still need to specify the file path C:\MyFiles\*.txt – Etienne Jun 29 '12 at 07:49
  • It works for me and a small sample file, you could post a snippet of your data file – jeb Jun 29 '12 at 08:43
  • If I make the code to look at only 1 file with only the word error in it still does not work hey. – Etienne Jun 29 '12 at 08:54
  • @Etienne - jeb misunderstood your requirement and thought you were trying to get the count of the number of lines that have either word. The solution should be simple - just remove the pipe and the find command from his answer. But... read [my answer](http://stackoverflow.com/a/11261016/1012053) as to why it might not be that simple. – dbenham Jun 29 '12 at 11:37