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 05:12
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Wonderful] [Statistics of the Number of Occurrences of Batch Processing Characters] DigestI View 23,259 Replies 41
Original Poster Posted 2006-11-29 08:26 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
### 代码实现
```batch
@echo off
setlocal enabledelayedexpansion
set "str=adadfdfseffserfefsefseetsdg"
set "chars=a b c d e f g h i j k l m n o p q r s t u v w x y z"
set max_count=0
set max_char=
for %%c in (%chars%) do (
set count=0
for /l %%i in (0,1,100) do (
set "char=!str:~%%i,1!"
if "!char!"=="%%c" (
set /a count+=1
)
)
if !count! gtr !max_count! (
set max_count=!count!
set max_char=%%c
)
)
echo 出现次数最多的字母是:%max_char%,出现次数:%max_count%
endlocal
```

### 代码解释
1. `@echo off`:关闭命令回显,使脚本执行时不显示命令本身。
2. `setlocal enabledelayedexpansion`:启用延迟环境变量扩展,以便在循环中正确获取更新后的变量值。
3. `set "str=adadfdfseffserfefsefseetsdg"`:设置要处理的字符串。
4. `set "chars=a b c d e f g h i j k l m n o p q r s t u v w x y z"`:定义所有可能的字母。
5. `for %%c in (%chars%) do`:遍历每个字母。
6. `set count=0`:初始化每个字母的计数为0。
7. `for /l %%i in (0,1,100) do`:循环遍历字符串的每个字符(假设字符串长度不超过100)。
8. `set "char=!str:~%%i,1!"`:获取字符串中指定位置的字符。
9. `if "!char!"=="%%c" ( set /a count+=1 )`:如果当前字符与遍历的字母相同,计数加1。
10. `if !count! gtr !max_count! ( set max_count=!count! & set max_char=%%c )`:如果当前字母的计数大于最大计数,更新最大计数和对应的字母。
11. `echo 出现次数最多的字母是:%max_char%,出现次数:%max_count%`:输出出现次数最多的字母及其次数。
12. `endlocal`:结束局部环境,恢复之前的环境变量状态。
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 2 Posted 2006-11-29 08:52 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Actually, there is ready-made code available for plagiarism in this topic in the forum. Brother 3742668 did it before, and I also helped others answer in the Wuyou Boot Forum. I won't provide the code for now. You all can discuss it first ^_^.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-11-29 09:11 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Suggestion: set "str=`-=\;',./~%!@#o^&*()_+{}|:<>?"abc123ABC00"

This kind of consideration for special characters is a bit challenging, oh

[ Last edited by vkill on 2006-11-30 at 04:43 AM ]
Floor 4 Posted 2006-11-29 09:33 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
There is a command called wc in Linux haha ha ha ha

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 5 Posted 2006-11-29 13:30 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Have a try^_^
@echo off
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdg

:loop
set str$=%str$% %str:~0,1%
set str=%str:~1%
if not "%str%" == "" goto loop

for %%i in (%str$%) do call :Scan1 %%i

pause
exit

:Scan1
for %%i in (%exclude%) do if "%1"=="%%i" goto :eof
set exclude=%exclude% %1
call :Scan2 %1
goto :eof

:Scan2
for %%i in (%str$%) do if "%1"=="%%i" set /a %1+=1
echo %1 !%1!
goto :eof


[ Last edited by zxcv on 2006-11-29 at 01:31 AM ]
Recent Ratings for This Post ( 5 in total) Click for details
RaterScoreTime
ccwan +3 2006-11-30 00:30
lxmxn +5 2006-11-30 00:55
namejm +4 2006-11-30 01:55
pengfei +4 2006-11-30 03:31
redtek +5 2006-12-30 07:03
Floor 6 Posted 2006-11-29 23:25 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Brother zxcv, the code on floor 5 is interesting~ Appreciate it~~~


set str$=%str$% %str:~0,1%
set str=%str:~1%
……


The above first "breaks down" the string in the str variable "adadfdfseffserfefsefseetsdg" into individual characters separated by spaces, for the convenience of the for loop to take character values one by one~ : )

(This technique of breaking down a continuous string into individual letters separated by spaces is very interesting~ : )



for %%i in (%str$%) do call :Scan1 %%i


When the string has been "broken down" into individual letters each separated by a space, it reads them one by one through for and passes the data to the :Scan1 label code segment in the form of a call statement with parameters.

At this time, the :Scan1 label code segment can use the %1 method to get the data called over and process it~ : )

(The purpose of breaking down the continuous string into individual letters separated by spaces is to facilitate the for to get individual letters)
(And for getting strings with spaces as delimiters makes use of the default value of the for Delims parameter)

……

Brother zxcv, please talk about the ideas and principles of the other parts of the code? 



(The most difficult thing is when "away from" the various functions that can be called at any time in high-level languages, (such as special functions for string interception, statistics, etc......)
(In this case, using internal commands and simple statements in batch processing can also complete the "transmission" of ideas. )

(Almost everything is like primitive operations, a few simple statement separations, a few Goto, a few SET...... can still complete the idea like simulating the "underlying" level. )

(In such a thinking process, everything is under the condition of no ready-made function support, using these simple commands to reproduce this complex process and the realization of the dream)
(In this process, you can experience and feel the "primitive" and the most direct way to reproduce the beauty of batch processing)


(All the points were given away yesterday, and I will make up the points for zxcv this afternoon~ : )
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
meiko +1 2007-04-22 05:07
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 7 Posted 2006-11-29 23:52 ·  中国 浙江 宁波 鹏博士宽带
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Special characters in cmd are always a headache, and there's no way to solve them.
I also posted a thread. There's really no better way to implement it, so I use a crude method to make do.


  1. @echo off
  2. setlocal ENABLEDELAYEDEXPANSION
  3. set str=adadfdfseffserfefsefseetsdg
  4. set /a m=0,n=0,l=0
  5. call :loop
  6. for /f "tokens=1,2 delims==" %%i in ('set 字符:') do (
  7. echo %%i=%%j
  8. if %%j GTR !l! set l=%%j & set m=%%i
  9. )
  10. echo.出现次数最多的%m%=%l%
  11. pause
  12. goto :EOF
  13. :loop
  14. call set m=%%str:~%n%,1%%
  15. if not defined m goto :EOF
  16. set /a "字符:%m%+=1"
  17. set /a n+=1
  18. goto loop
Posted by 无奈何 on November 29, 2006 at 10:50
Recent Ratings for This Post ( 7 in total) Click for details
RaterScoreTime
ccwan +3 2006-11-30 00:30
lxmxn +5 2006-11-30 00:56
namejm +4 2006-11-30 02:40
pengfei +4 2006-11-30 03:32
IceCrack +4 2006-11-30 08:14
redtek +5 2006-12-30 07:03
26933062 +2 2007-01-10 09:35
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 8 Posted 2006-11-30 00:08 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
:Scan1
for %%i in (%exclude%) do if "%1"=="%%i" goto :eof
set exclude=%exclude% %1
call :Scan2 %1
goto :eof

Using "for %%i in (%exclude%) do if "%1"=="%%i" to filter out the same characters, if there is a duplicate, return "for %%i in (%str$%) do call :Scan1 %%i" to filter the next character. The new character not in %exclude% is added with "set exclude=%exclude% %1" (this is when I was sorting mobile phone ringtones to filter the selected numbers)

:Scan2
for %%i in (%str$%) do if "%1"=="%%i" set /a %1+=1
echo %1 !%1!
goto :eof

The filtered characters are calculated one by one for the number of occurrences. After self-calculation, "!%1!" is used with delayed variables to display the number of occurrences

The reason for using "set /a %1+=1" is to provide usage for a single character later

If you just want to display, change it to:
:Scan1
for %%i in (%exclude%) do if "%1"=="%%i" goto :eof
set exclude=%exclude% %1
set N=
call :Scan2 %1
goto :eof

:Scan2
for %%i in (%str$%) do if "%1"=="%%i" set /a N+=1
echo %1 %N%
goto :eof

You can do without delayed variables
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2006-12-30 07:04
Floor 9 Posted 2006-11-30 00:22 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Helplessly, the code of the moderator and brother zxcv shines with the crystallization of every line of thinking ~~~
It's really wonderful ~~
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 10 Posted 2006-11-30 00:30 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
I'll help Brother redtek add points! The two of you have great comments. Unfortunately, I don't have points right now. Next time then.

[ Last edited by ccwan on 2006-11-30 at 12:35 AM ]
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 11 Posted 2006-11-30 00:37 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Borrowing the script from Moderator Wunaike, but it hasn't been sorted alphabetically yet ^_^:

@echo off
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdg

:loop
set str$=%str$% %str:~0,1%
set str=%str:~1%
if not "%str%" == "" goto loop

for %%i in (%str$%) do call :Scan1 %%i
echo The most frequent occurrence: %max%=%maxN%
pause
exit

:Scan1
for %%i in (%exclude%) do if "%1"=="%%i" goto :eof
set exclude=%exclude% %1
call :Scan2 %1
goto :eof

:Scan2
for %%i in (%str$%) do if "%1"=="%%i" set /a %1+=1
if !%1! GTR !maxN! set maxN=!%1! & set max=%1
echo Character: %1=!%1!
goto :eof
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2006-11-30 02:31
Floor 12 Posted 2006-11-30 00:44 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
If combined with sed grep, it is much simpler and appears
Floor 13 Posted 2006-11-30 00:46 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
vkill use sed to make one~ :)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 14 Posted 2006-11-30 00:53 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by redtek at 2006-11-30 00:46:
vkill make one with sed~:)

Hehe~ grep -c can be used, because without using third-party tools, special characters can't be handled at all, I think
Floor 15 Posted 2006-11-30 02:30 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
I also come to a section, borrow the code segment with the space-separated letters of zxcv brother's :loop ~:)


@echo off
set str=adadfdfseffserfefsefseetsdg

:loop
set str$=%str$% %str:~0,1% && set str=%str:~1%
if not "%str%" == "" goto loop

call :start %str$%
set . & goto :eof

:start
if == ( goto :eof ) else ( set /a .%1+=1 )
shift
goto :start
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
namejm +4 2006-11-30 02:48
zh159 +4 2006-11-30 03:00
youxi01 +2 2006-11-30 06:08
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Forum Jump: