[英]How to search ocuurence of a string across all files in a directory using batch
我需要在文件夹中存在的所有 html 文件中搜索字符串“样式”。 在输出中,我需要文件名和字符串“style”的出现次数。 例如输出会像
a.html 3
b.html 6
c.html 7
I have tried using
find /c "style" *.html
问题是在一行中,它只读取字符串一次,然后移动到下一行,即如果文件有内容: style style style style style 它只会读取计数为 2。 我需要数为 5,搜索也应该在整个文件夹中。 请帮忙
只要文件名不包含空格、特殊批处理字符或算术运算符(如-
),这应该有效。
@echo off
setlocal EnableDelayedExpansion
set "word=style"
for /F "tokens=1* delims=:" %%a in ('findstr "%word%" *.html') do (
set "line=%%b"
set "line=!line:"=!"
for %%c in ("!line:%word%=" "!") do set /A "count[%%a]+=1"
set /A "count[%%a]-=1"
)
for /F "tokens=2,3 delims=[]=" %%a in ('set count[') do echo %%a %%b
for /F "delims=" %G in ( 'findstr /S /M /I "style" "%CD%\*.html" ') do @find /c "style" "%~G" | findstr /V /R "^S"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.