SoFunction
Updated on 2025-04-09

Batch processing that counts the number of repetitions and sorts


@echo off
:: Purpose:
There is only one number per row in ::. The number of repetitions of each number is counted and sorted from high to low according to the number of repetitions
:: Ideas:
:: First use sort to sort all numbers, then count the number of repetitions, and write to the temporary file in the format of number + repetitions;
:: Extract the number of repeated times, create a file named after the number with the number as the length, use dir to sort the file name (i.e. the number of repeated times) and write it;
:: Extract the contents by row, and then find the records matching the ones in the text and write the result;
:: This solution will generate a large number of temporary files, but it is more efficient
::
::
set num=-1
sort<>
cd.>
cd.>
cd.>
:: Statistics of repetitions
setlocal enabledelayedexpansion
for /f %%i in () do (
set /a num+=1
set second=!first!
set first=%%i
if not "!second!"=="" if !second! neq !first! (>> echo !second! !num!&set num=0)
)
>> echo %first% %num%
:: Sort the number of repetitions
md tmp && pushd tmp
for /f "tokens=2" %%i in (..\) do (
cd.>%%i
for /l %%j in (1,1,%%i) do echo.>>%%i
)
>..\ dir /o-s /b
:: Extract records by number of repetitions
for /f %%i in (..\) do (
>>..\ findstr " %%i$" ..\
)
popd && rd /q /s tmp
del
start
goto :eof
Other solutions for counting the number of characters (no temporary files are generated)
@echo off
:: counts the number of occurrences of each character and finds the character with the most occurrences
:: Ideas:
:: By extracting the characters on each bit, assigning certain dynamic variables starting with characters:
:: If the variable name is the same, it will be added once, and then, extract it at one time through the set character: command
:: All dynamic variables starting with characters: are handed over to the for statement for processing. Set is used cleverly
:: No need to generate temporary files and be arranged in ascending order of letters
::
::
::
setlocal ENABLEDELAYEDEXPANSION
set str=adadfdfseffserfefsefseetsdmg
set /a m=0,n=0,l=0
call :loop
:: The following are the characters that appear the most times
for /f "tokens=1,2 delims==" %%i in ('set character:') do (
echo %%i=%%j
if %%j GTR !l! set l=%%j& set m=%%i
)
echo. The most common occurrences of %m%=%l%
pause
goto :EOF
:loop
call set m=%%str:~%n%,1%%
if not defined m goto :EOF
set /a "character: %m%+=1"
set /a n+=1
goto loop
@echo off
:: Statistics the number of characters
:: Ideas:
:: First disassemble the string into a single character string separated by spaces.
:: Then use the for statement to detect the number of times each character appears in the string
:: This method does not require generating temporary files and as shown in the string
:: Displayed in sequence
::
::
::
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdg
rem disassemble string
:analyze
set str_tmp=%str_tmp% %str:~0,1%
set str=%str:~1%
if not "%str%" == "" goto analyze
rem
for %%i in (%str_tmp%) do call :exclude %%i
pause
exit
:exclude
for %%i in (%counted%) do if "%1"=="%%i" goto :eof
set counted=%counted% %1
call :count %1
goto :eof
:count
for %%i in (%str_tmp%) do if "%1"=="%%i" set /a %1+=1
echo %1 !%1!
goto :eof
@echo off
:: Count the number of characters that appear
:: Ideas:
:: Disassemble the string and separate the new string with spaces
:: Call different parameters through shift and use
::set to name the variable, the variable name has a unified beginning
:: Finally, these variables are displayed by set
::
::
::
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdg
:loop
set str_tmp=%str_tmp% %str:~0,1% && set str=%str:~1%
if not "%str%" == "" goto loop
call :start %str_tmp%
set .
echo:%max%=%maxN%
pause
exit
:start
if [%1]==[] ( goto :eof ) else ( set /a .%1+=1 )
if !.%1! GTR !maxN! set maxN=!.%1!&& set max=.%1
shift
goto :start
@echo off
:: Based on the above scheme, the simplest code is as follows
::
::
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdgadadfdfseffserfefsefseetsdga
:loop
set str$=%str$% %str:~0,1%&set str=%str:~1%
if not "%str%" == "" goto loop
for %%n in (%str$%) do (
set /a .%%n+=1
if !.%%n! GTR !maxN! set maxN=!.%%n!&&set max=%%n)
set .
echo:%max%=%maxN%
pause
exit
@echo off&setlocal
After :: sort, count the number of repetitions by comparing whether the content obtained this time is equal to the content of the last time
:: How to save the content of this and last time requires a lot of skills
:: Note that the initial value of the number of times should be set to 1. The for statement should not be followed by the parentheses of the for statement.
::
::
set /a n=1
for /f %%a in ('type ^|sort') do (
call :pp %%a
)
:pp
if not defined bb goto b
if "%bb%"=="%1" (set /a n+=1) else (>> echo %bb% %n% times&set /a n=1)
:b
set bb=%1
goto :eof
@echo off&setlocal enabledelayedexpansion
:: Code with sorting function
:: Use for /l to control the character length of each findstr.
:: Then sort the same length with sort, thus breaking through
::sort can only be sorted by character bit size
::
::
set a=[0-9]
for /l %%a in (1,1,3) do (
call :pp !a!
set a=!a![0-9]
)
goto c
:pp
for /f %%x in ('findstr "^%1$" ^|sort') do @echo %%x >>
goto :eof
:c
set /a n=1
for /f %%a in ('type ') do (
call :pp %%a
)
:pp
if not defined bb goto b
if "%bb%"=="%1" (set /a n+=1) else (>> echo %bb% %n% times&set /a n=1)
:b
set bb=%1
goto :eof