中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-09 19:03
47,811 topics / 349,896 posts / today 1 new / 48,254 members
DOS批处理 & 脚本技术(批处理室) » How to count the number of times a certain string appears in a text?
Printable Version  3,001 / 5
Floor1 namejm Posted 2006-06-14 19:39
荣誉版主 Posts 1,737 Credits 5,226 From 成都
For example, there is a text file with several lines of content, such as:
1,2,3,4
7,8,9,10
……
How to count the number of commas in it?
Floor2 bagpipe Posted 2006-06-15 10:53
银牌会员 Posts 425 Credits 1,144 From 北京
In fact, I don't have a very good way for this kind of question. I think the limitations are also very big, and I haven't found a good way to solve it. But the landlord can try the following code:

@echo off
setlocal enabledelayedexpansion
set a=
for /f "delims=" %%a in (要检索的文件.txt) do set a=!a!%%a
:back
set /a b+=1
set a=%a:*,=%
if "%a%"=="这里是最后一个逗号后面内容" goto end
goto back

:end
echo There are %b% commas
Floor3 piziliu2004 Posted 2006-06-15 13:50
中级用户 Posts 139 Credits 321
to: bAGPIPE

What does set a=%a:*,=% mean?
Floor4 kcdsw Posted 2006-06-15 15:23
中级用户 Posts 179 Credits 404
Look at help

set 变量名=%变量:1=2%
This means to replace 1 in front with 2 after =.
Floor5 namejm Posted 2006-06-15 16:07
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Thanks to bagpipe, I can use it with a little modification based on your code.
——The limitation you mentioned should be that the content after the last comma in "if "%a%"=="here is the content after the last comma" goto end" needs to be entered manually, right?
Floor6 bagpipe Posted 2006-06-16 08:30
银牌会员 Posts 425 Credits 1,144 From 北京
The limitations I mentioned are not what Brother namejm said. In fact, I can get the content after the last comma. The limitation I mentioned is because you assign all the characters in the file to a variable, and then how many characters this variable can bear at most. If there is no content after the last comma, this situation will not be the case.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023