China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 10:42
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to count the number of times a certain string appears in a text? View 2,925 Replies 5
Original Poster Posted 2006-06-14 19:39 ·  中国 四川 雅安 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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?
Floor 2 Posted 2006-06-15 10:53 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
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
Floor 3 Posted 2006-06-15 13:50 ·  中国 江苏 苏州 电信
中级用户
★★
过度热情
Credits 321
Posts 139
Joined 2006-03-21 17:19
20-year member
UID 52521
Gender Male
Status Offline
to: bAGPIPE

What does set a=%a:*,=% mean?
Floor 4 Posted 2006-06-15 15:23 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
Look at help

set 变量名=%变量:1=2%
This means to replace 1 in front with 2 after =.
Floor 5 Posted 2006-06-15 16:07 ·  中国 四川 雅安 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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?
Floor 6 Posted 2006-06-16 08:30 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
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.
Forum Jump: