如何计算findstr结果



我使用"findstr"在很多文件中查找某些常用值,并获得包含它的所有文件的列表。有没有一种方法可以计算这些结果,这样我就不必手动执行,或者像在CMD中启用行号一样?

我通常会做这样的事情:

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

这将通过管道将命令行输出导入名为results.txt的新文本文件

转到Windows资源管理器。查找results.txt文件
右键单击-->编辑Ctrl-A选择所有

查看记事本的最底部,查看的总行数

I use 'findstr' to find certain common values in a lot of files and I get a list of all files containing it.
所以您可能使用类似findstr /m "searchstring" *.*的东西

Is there a way to count those results so I don't have to do it manually
是,通过find馈送,可以计数:

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

or like enable row numbers in CMD?
仅出于学术原因(您已经有了自己的号码(:

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

万一您需要它作为变量:使用for /f循环来捕获结果:

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

(%a是命令行语法。在批处理文件中使用%%a(

以防万一Compo是对的:获取每个文件中搜索字符串的出现次数:

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

(2>0抑制文件夹的错误消息(find试图搜索文件夹,但由于它们不是文件,所以失败得很惨[从技术上讲,它们是文件,但这超出了您的问题范围](

最新更新