How to count characters in a text file using batch

2019-09-05 21:13发布

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条回答
淡お忘
2楼-- · 2019-09-05 22:11

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.
查看更多
登录 后发表回答