How to count characters in a text file using batch file

旧巷老猫 提交于 2020-01-17 09:11:08

问题


I want to count all the characters of a certain text. However in my code it only counts single string. But the requirement needs to count all characters including whites spaces and new line.. For example:

Hello World!
How are you today?
Hope you are okay

How am i going to do that in my code?Thanks. My code:

@ECHO OFF

for %%i in (y.txt) do @set count=%%~zi
REM Set "string" variable
SET string=(Hello World
            How are you today?
            Im fine..) // i want to read these string because my code only read single string
REM Set the value of temporary variable to the value of "string" variable
SET temp_str=%string%
REM Initialize counter
SET str_len=0

:loop
if defined temp_str (
REM Remove the first character from the temporary string variable and increment 
REM counter by 1. Countinue to loop until the value of temp_str is empty string.
SET temp_str=%temp_str:~1%
SET /A str_len += 1
GOTO loop
)

REM Echo the actual string value and its length.
ECHO %string% is %str_len% characters long!

回答1:


this is all, you need:

@ECHO OFF
set /p "file=enter filename: "
for %%i in (%file%) do @set count=%%~zi
echo this file has %count% characters including whitespaces and special chars like line-feed/carriage-return etc.


来源:https://stackoverflow.com/questions/29103677/how-to-count-characters-in-a-text-file-using-batch-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!