简体   繁体   中英

How to search ocuurence of a string across all files in a directory using batch

I need to search a string "style" across all html files present in a folder. In the output I need file name and count of the occurrence of the string "style". for eg output will be like

a.html  3
b.html   6
c.html  7
I have tried using
find /c "style" *.html

The problem is in one line it reads the string only once and moves to next ie if a file has content: style style style style style It will read count as 2 only. I need count as 5 and that too search should be across the folder. Please help

This should work, as long as the file names does not contain spaces, special Batch characters nor arithmetic operators (like - ).

@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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM