How to count findstr results

Viewed 20

I use 'findstr' to find certain common values in a lot of files and I get a list of all files containing it. Is there a way to count those results so I don't have to do it manually, or like enable row numbers in CMD?

2 Answers

I would typically do something like:

c:\>findstr some-string file.txt > results.txt

This will pipe the command line output into a new text file named results.txt

Go to Windows Explorer.   Find results.txt file.
Right-click --> Edit Ctrl-A to select all

Look at very bottom of Notepad to see the total number of lines.

I use 'findstr' to find certain common values in a lot of files and I get a list of all files containing it.
So you probably use something like findstr /m "searchstring" *.*

Is there a way to count those results so I don't have to do it manually
yes, feed it through find, which can count:

findstr /m "searchstring" *.* | find /c /v ""

or like enable row numbers in CMD?
just for academic reasons (you already have your number):

findstr /m "searchstring" *.* | find /n /v ""

Just in case you need it as variable: use a for /f loop to catch the result:

for /f %a in ('findstr /m "searchstring" *.* ^| find /c /v ""') do set count=%a

(%a is command line syntax. Use %%a in a batch file)

Just in case Compo is right: to get the occurrences of the searchstring per file:

find /c "searchstring" *.* 2>nul

(2>0 suppresses errormessages for folders (which find tries to search, but miserably fails because they are not files [well, technically they are, but that's out of scope for your problem]))

Related