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-20 20:46
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch processing (merging, statistics) of a large number of text files View 2,864 Replies 5
Original Poster Posted 2006-10-04 23:36 ·  中国 北京 海淀区 联通
新手上路
Credits 6
Posts 1
Joined 2006-10-03 23:58
19-year member
UID 64464
Status Offline
I have a series of text files (596 files) and want to do the following processing:
1. Merge the contents of specified files (such as 001 to 180), preferably according to input like "Please enter the files you want to merge", and write to a new file new001.txt;
2. Count the frequency of all Chinese characters (preferably including punctuation marks) in new001, and write the result to result001.txt.
I'm a novice, please trouble experts to give some guidance.
Floor 2 Posted 2006-10-04 23:58 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
One thing that is quite annoying is: Is it a bit of a waste to use batch processing for many one-time, non-repeating tasks?

Personally, I think the user's problem can be well solved with Word. Do you really have to handle it in the cmd black box to feel comfortable?

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 3 Posted 2006-10-05 00:12 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
The frequency of all Chinese characters appearing = =b
I want to know, what's the use of you doing this statistics.
Floor 4 Posted 2006-10-05 00:25 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
It's not impossible to meet the LZ's requirements. But if you want to filter so many characters, you can imagine how inefficient it will be. The code will also be very long and tedious~~~!
Floor 5 Posted 2006-10-05 00:27 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
For your first question, the following code can be used to implement it:

@echo off
:: Please ensure that there are no sensitive characters under CMD in each text file
:: 180 can be modified to the 596 you want
:: To get the function of merging specified files, please modify the following code by yourself
del /a /q /f new001.txt 2>nul
setlocal enabledelayedexpansion
for /l %%i in (1,1,180) do (
set num=%%i
if %%i lss 10 set num=00%%i
if %%i geq 10 if %%i lss 100 set num=0%%i
for /f "delims=" %%j in (!num!.txt) do >>new001.txt echo%%j
)
start new001.txt

Hitme once did the statistics on the frequency of Chinese character occurrence, but it seems that the result is not very accurate. Here is his code for everyone to discuss

Bored, just messing around, test
Function: Count the number of times each word appears in the text
Usage:
Under cmd
word_num file name
Statistical result style:
0152 fixed
0108 added
0094 in
0091 to
0070 will
0069 not
0062 problem
0059 the
0056 bar
0054 support
0054 at
0053 and
0050 for
0047 menu
0046 tab
0042 when
0041 new
0040 option
0040 changed
0038 myie2
0037 plugin
0036 favorite
0035 toolbar
0035 on
Code
@echo off
cls
echo.
echo.-----------------------------
echo....Counting in progress.....Please wait a moment...
echo.-----------------------------
echo.
if "%*" "" goto :syntax
if "%*" "/?" goto :syntax
if "%*" "/" goto :syntax
if "%*" "?" goto :syntax
if /i "%*" "/help" goto :syntax
:: /* Split the text into one word per line */
setlocal ENABLEDELAYEDEXPANSION
del end.txt tmp*.txt 2>nul
set /a m=0

:loop
set /a m+=1
for /f "tokens=%m% delims=.,:;/()!'@=\<> " %%i in (%*) do (
set n=%%i
set n=!n:"=!
echo.1 !n!>>tmp.txt
)
if "%m%" "200" goto:add
goto:loop
endlocal
:add
:: /* Summarize by category */
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1,2" %%i in ('sort tmp.txt^|findstr /r "^1\ "') do (
if /i "!y!" "%%j" (
set /a x+=1) else (if "!x!" neq "" (
if !x! lss 10 (echo.000!x! !y!>>tmp1.txt) else (
if !x! lss 100 (echo.00!x! !y!>>tmp1.txt) else (
if !x! lss 1000 (echo.0!x! !y!>>tmp1.txt))))
set y=%%j
set x=%%i)
)
if !x! lss 10 (echo.000!x! !y!>>tmp1.txt) else (
if !x! lss 100 (echo.00!x! !y!>>tmp1.txt) else (
if !x! lss 1000 (echo.0!x! !y!>>tmp1.txt)))
endlocal
sort /r tmp1.txt>tmp2.txt
:ex
:: /* Convert uppercase to lowercase */
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (tmp2.txt) do (
set ex=%%i
set ex=!ex:A=a!
set ex=!ex:B=b!
set ex=!ex:C=c!
set ex=!ex:D=d!
set ex=!ex:E=e!
set ex=!ex:F=f!
set ex=!ex:G=g!
set ex=!ex:H=h!
set ex=!ex:I=i!
set ex=!ex:J=j!
set ex=!ex:K=k!
set ex=!ex:L=l!
set ex=!ex:M=m!
set ex=!ex:N=n!
set ex=!ex:O=o!
set ex=!ex:P=p!
set ex=!ex:Q=q!
set ex=!ex:R=r!
set ex=!ex:S=s!
set ex=!ex:T=t!
set ex=!ex:U=u!
set ex=!ex:V=v!
set ex=!ex:W=w!
set ex=!ex:X=x!
set ex=!ex:Y=y!
set ex=!ex:Z=z!
echo.!ex!>>end.txt
)
del tmp*.txt
start end.txt

:syntax
cls
echo.
echo.word_num.cmd
echo.
echo.written by hitme 2005.9.2
echo.Usage:
echo.word_num file name
echo.
echo.Example:
echo.word_num License.txt
echo.
To count Chinese characters, the usage is the same as the top floor. Try to figure out how to make it vertical in a column
Code:
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (%1) do (
set n=%%i
echo.!n!>tmp.txt
echo.x>>tmp.txt
for /f "tokens=1 delims=:" %%a in ('findstr /o "x" tmp.txt') do set lenth=%%a
for /l %%a in (0,1,!lenth!) do (
set m=!n:~%%a,1!
if "!m!" neq "" echo.1 !m!>>tmp1.txt
)
)

:add
for /f "tokens=1,2" %%i in ('sort tmp1.txt') do (
if "!y!" "%%j" (
set /a x+=1) else ( if "!x!" neq "" (
if !x! lss 10 (echo.000!x!
!y!>>tmp2.txt) else (
if !x! lss 100 (echo.00!x!
!y!>>tmp2.txt) else (
if !x! lss 1000 (echo.0!x!
!y!>>tmp2.txt))))
set y=%%j
set x=%%i)
)
if !x! lss 10 (echo.000!x! !y!>>tmp2.txt) else (
if !x! lss 100 (echo.00!x! !y!>>tmp2.txt) else (
if !x! lss 1000 (echo.0!x! !y!>>tmp2.txt)))
endlocal
sort /r tmp2.txt>end.txt
del tmp*.txt
start end.txt
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2006-10-05 10:34 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Originally posted by oxus at 2006-10-4 23:36:
I have a series of text files (596 files), and I want to do the following processing:
1. Merge the content of specified files (such as from 001 to 180). It is best according to input, for example "Please enter the files you want to merge...



What do you want:

A、Help you complete the work tasks you encounter? (Do it all for you)
B、Help you master the knowledge needed to complete the work? (Help you master batch processing knowledge, and you learn to do it yourself)
Forum Jump: