正如马克所说,find
返回文件中匹配的行数,而不是一行中单个字符串的数量。为此,您需要使用另一种方法,例如:
@echo off
setlocal EnableDelayedExpansion
set /a count = 0
for /f "delims=" %%a in ('dir "%~1" /a:-d /b') do (
for /F "delims=" %%b in ('find %2 ^< "%%a"') do call :next "%%b" "%~2"
)
echo found %count% occurances of "%~2"
pause
GOTO:EOF
:next
set num=0
set "line=%~1"
:nextMatch
set "line2=!line:*%~2=!"
if "!line2!" equ "!line!" goto endMatchs
set /A num+=1
set "line=!line2!"
if defined line goto nextMatch
:endMatchs
set /a count=count+num
例如:
C:> type 1.txt
An example of text file.
This example line have two "example" words.
End of example.
C:> test 1.txt "example"
found 4 occurances of "example"