查找在文件中出现的字符串,并显示“文件名-计数".通过批处理文件 [英] Find occurrences of a string in files and display "filename - count" through batch file

查看:65
本文介绍了查找在文件中出现的字符串,并显示“文件名-计数".通过批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

批处理文件以搜索目录中的每个子文件夹和每个文件,并计算每个文件中存在特定字符串的次数.

Batch file to search every single subfolder and every single file inside a directory and count the number of times a particular string is present in each file.

如果输出为文件名-计数",将很有用.

Would be useful if output is "filename - count".

可以找到/c"Microsoft" * .txt如果所有文件都在一个文件夹中,则此方法有效.

Can do find /c "Microsoft" *.txt This works if all the files are in one folder.

如何使find循环遍历所有子文件夹及其每个文件,并显示相同的结果.

How do you make the find loop through all the subfolders and each of its files and display the same result.

Findstr具有/s来执行此操作,在find上不起作用.

Findstr has /s which does that, doesnt work on find.

推荐答案

从命令行:

for /F "delims=" %G in ('findstr /I /S /M "Microsoft" "%CD%\*.txt"') do @find /I /C "Microsoft" "%~G" | findstr /V /R "^$"

通过批处理脚本:

set "_srch=Microsoft"
for /F "delims=" %%G in ('
       findstr /I /S /M "%_srch%" "%CD%\*.txt"') do (
    find /I /C "%_srch%" "%%~G" | findstr /V /R "^$"
)

省略%CD%\ ,您将获得相对路径.

Omitting the %CD%\ you will get relative paths.

要从 find 输出(命令行)中删除 ---------- :

To get rid of ---------- from find output (command line):

for /F "delims=" %G in ('findstr /I /S /M "Microsoft" "%CD%\*.txt"') do @for /F "tokens=1,*" %H in ('find /I /C "Microsoft" "%~G"') do @echo %I

资源:为/?,查找/? findstr/? set/? set/?键入转到 Windows CMD命令行的AZ索引.

Resources: type for /?, find /?, findstr /?, set /? or go to An A-Z Index of the Windows CMD command line.

这篇关于查找在文件中出现的字符串,并显示“文件名-计数".通过批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆