如何使用批处理文件在一个文本文件的字符计数 [英] How to count characters in a text file using batch file

查看:176
本文介绍了如何使用批处理文件在一个文本文件的字符计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想算一定文本的所有字符。然而,在我的code这只能算作一个字符串。但要求需要计算所有字符,包括白人的空间和新的生产线。例如:

 世界,你好!
你今天好吗?
希望大家都还好

我要怎么做,在我的code?谢谢。
我的code:

  @ECHO OFF为%%我在(y.txt)做@set数= %%〜滋
REM集字符串变量
SET字符串=(世界您好
            你今天好吗?
            我没事..)//我想读这些字符串,因为我的code只读单一的字符串
REM临时变量的值设置为字符串变量的值
SET temp_str =%字符串%
REM初始化计数器
SET str_len = 0:循环
如果定义temp_str(
REM从临时字符串变量和增量删除的第一个字符
1。Countinue循环REM计数器,直到temp_str的值是空字符串。
SET temp_str =%temp_str:〜1%
SET / A str_len + = 1
GOTO循环
)REM回声实际的字符串值,其长度。
ECHO%字符串%是%str_len%字符!


解决方案

这是一切,你需要:

  @ECHO OFF
集/ P档=输入文件名:
对于%% i的(%文件%)也算@set = %%〜滋
回声这个文件%计%字符,包括空格和特殊字符像换行/回车等。

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!

解决方案

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.

这篇关于如何使用批处理文件在一个文本文件的字符计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆