windows-xp - 批处理脚本 - 计算文件中字符的实例数

标签 windows-xp count batch-file

在 Windows XP 中使用批处理脚本(.bat 文件),我将如何读取文本文件并查找存在多少个字符实例?

例如,我有一个包含以下内容的字符串:

""OIJEFJ"JOIEJKAJF"""LKAJFKLJEIJ""JKLFJALKJF"LKJLKFA""""LKJKLFJLKADJF

我希望它计算文件中有多少个 " 并返回计数。

最佳答案

让我们开始计算一行中的字符数。首先是缓慢而清晰的方法:

set i=-1
set n=0
:nextChar
    set /A i+=1
    set c=!theLine:~%i%,1!
    if "!c!" == "" goto endLine
    if !c! == !theChar! set /A n+=1
    goto nextChar
:endLine
echo %n% chars found

现在是快速而神秘的方法:

call :strLen "!theLine!"
set totalChars=%errorlevel%
set strippedLine=!theLine:%theChar%=!
call :strLen "!strippedLine!"
set /A n=totalChars-%errorlevel%
echo %n% chars found
goto :eof

:strLen
echo "%~1"> StrLen
for %%a in (StrLen) do set /A StrLen=%%~Za-4
exit /B %strLen%

最后是计算文件中字符的方法:

set result=0
for /F "delims=" %%a in ('findstr "!theChar!" TheFile.txt') do (
    set "theLine=%%a"
    place the fast and cryptic method here
    set /A result+=n
)
echo %result% chars found

关于windows-xp - 批处理脚本 - 计算文件中字符的实例数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7983925/

相关文章:

java - Solr 一次数百万个搜索查询

WindowsXP 上的 JavaFX 错误

c# - 使用 .NET 2.0 在 XP 上运行 .NET 4.5 程序

Java hibernate 分离条件、计数/拥有、查询

windows - 使用 .BAT 脚本查找文件匹配和变量分配

linux - 带输入的批处理脚本。 ADB 命令、FireStick

node.js - nodejs - LDAP 客户端安装错误(Windows XP 机器)

c# - 如何以编程方式检索 "Program Files"文件夹的实际路径?

python - 如果满足某些条件,如何从另一个字典创建字典

python - Pandas 依靠柱子