批处理 计算在文件中 某个字符串出现的次数

这是我的代码
@echo off
set n=0
for %%a in (xxxx.*) do (
for /f "usebackq delims=" %%b in ("%%a") do (
for %%c in (%%b) do (
if %%c==nice set /a n+=1
)
)
goto :end
)
:end
echo nice共出现:%n%次
当我想把nice换成比如 nice girl 的时候就不行了 中间多了个空格 不知道该怎么处理 求大神指导

第1个回答  2013-12-11
@echo off
set n=0
for %%a in (xxxx.*) do (
for /f "usebackq delims=" %%b in ("%%a") do (
rem for %%c in (%%b) do (
if "%%b"=="nice girl" set /a n+=1
)
)
goto :end
)
:end 
echo nice共出现:%n%次
pause

加引号

第2个回答  2021-06-25
不清楚你的实际文件/情况,仅以问题中的样例/说明为据;以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的文件放一起双击运行
<# :
cls&echo off
rem 统计一个指定字符串在一个txt文本文件里出现的次数
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::GetEncoding('GB2312'))))"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>
$txtfile="xxx.txt";
$findword="指定字符串";
if(-not (test-path -liter $txtfile)){write-host ('"'+$txtfile+'" 未找到');exit;};
$text=[IO.File]::ReadAllText($txtfile, [Text.Encoding]::GetEncoding('GB2312'));
$index=0;
$count=0;
while(($index=$text.IndexOf($findword, $index)) -ne -1){
$count++;
$index=$index+$findword.Length;
}
write-host $count;
第3个回答  2013-12-11
@echo off
set n=0
for %%a in (xxxx.*) do (
for /f "usebackq delims=" %%b in ("%%a") do (
set nice=
for %%c in (%%b) do (
if %%c==girl if defined nice set /a n+=1
if %%c==nice ( set nice=1 ) else set nice=
)
)
goto :end

:end 
echo nice girl共出现:%n%次
pause

查找nice girl在文章中出现的次数

第4个回答  2013-12-11
加引号 if "%%c"=="nice girl"追问

我试过了 没用 他没办法识别

追答

嗯前面for 内容有空格就变了,如果是一行最多出现一次,帮你改写一个简单些的

 @echo off
set "str=nice girl"
for /f %%i in ('findstr /c:"%str%" xxx.*')do set/a n+=1
echo %str%共出现%n%次
pause

本回答被提问者和网友采纳
相似回答