Давайте начнем считать символы в строке.Сначала медленный и понятный метод:
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