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-07-05 18:34
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to read multiple text files line by line to achieve text merging View 2,205 Replies 15
Original Poster Posted 2008-03-29 20:17 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
Read multiple text files line by line into a new file (that is, merging text files) (requires that empty lines, leading and trailing blanks in the file can be output as they are). I wrote one but the empty lines are ignored, and when encountering blank lines containing spaces or tabs, it writes "echo 已经关闭" to the file. I don't know how to solve these problems?

@echo off
setlocal EnableDelayedExpansion REM Enable delayed variables=%
dir/b/o:n *.txt>filelist.tmp REM Get file list=%
type.>HBfile.txt REM First create the result file=%
for /f "usebackq delims=" %%a in ("filelist.tmp") do ( REM Get a file name=%
set filename=%%a REM Assign the file name to the variable=%
call :sub !filename! REM Call the subroutine with parameters=%
)
del filelist.tmp REM Delete the temporary file after completion=%
goto :eof REM Exit=%
:sub
for /f "usebackq delims=" %%i in ("%1") do (
echo %%i>>HBfile.txt) REM Write the obtained content to the result file=%
echo.>>HBfile.txt REM Add a blank line after each file is completed=%
goto :eof REM Exit subroutine=%
一个专搜注册信息的链接注册信息搜索--历史是一个旋转的车轮,它的轨迹总是那样的相似回复后可见
Floor 2 Posted 2008-03-29 20:41 ·  中国 陕西 汉中 电信
初级用户
Credits 136
Posts 49
Joined 2007-01-04 08:28
19-year member
UID 75413
Gender Male
Status Offline
Use the command "copy HBfile.txt+newfile.txt HBfile.txt"
Floor 3 Posted 2008-03-29 20:46 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
I think:
1. You can use copy copy a.txt +b.txt+c.txt+...........+n.txt m.txt
2. Use find for %%i in (a,b,c,................,n) do (
for /f %%j "skip=2 tokens=*" in ('find /v "******" %%i.txt') do (
echo %%j >>m.txt
)
)

[ Last edited by zw19750516 on 2008-3-29 at 08:52 PM ]
批处理之家新域名:www.bathome.net
Floor 4 Posted 2008-03-29 20:56 ·  中国 福建 福州 连江县 电信
中级用户
★★
Credits 356
Posts 115
Joined 2004-07-27 00:00
21-year member
UID 29114
Gender Male
Status Offline
@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do (more "%%i" >>合并.txt)
pause
Floor 5 Posted 2008-03-29 22:32 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
Thanks everyone, I overcomplicated the problem.
My original intention was to create a batch script for merging and splitting text files. When merging, I want to insert a separator line between each file so that it can be used as a marker when splitting later. This marker could be something like:
-----------------*.TXT-------------------
Later, when splitting, I first extract the file name from this marker, then write the next part of the content into this file, thus completing the splitting of one file. In this way, after merging multiple files and then splitting them, they can be completely restored to their original state.

However, I haven't written this batch script yet....
Because splitting files seems to be more difficult?

[ Last edited by zzhh612 on 2008-3-29 at 10:37 PM ]
一个专搜注册信息的链接注册信息搜索--历史是一个旋转的车轮,它的轨迹总是那样的相似回复后可见
Floor 6 Posted 2008-03-29 22:45 ·  中国 福建 福州 连江县 电信
中级用户
★★
Credits 356
Posts 115
Joined 2004-07-27 00:00
21-year member
UID 29114
Gender Male
Status Offline
@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do (
echo -----------------%%~nxi-------------------
more "%%i"
echo -----------------%%~nxi-------------------
)>>Merge.txt
Floor 7 Posted 2008-03-29 22:53 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
Thanks to balinger's code.

Your code meets my meaning, but I can't handle splitting it.
Mainly to extract the file name from it and extract the text between two markers, this must be read line by line.
This also involves the problem of empty lines and blank lines.
一个专搜注册信息的链接注册信息搜索--历史是一个旋转的车轮,它的轨迹总是那样的相似回复后可见
Floor 8 Posted 2008-03-29 22:57 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
### Display text between two regular expressions (inclusive)
sed -n '/Iowa/,/Montana/p'

This should be able to extract the content between filenames, right?
Floor 9 Posted 2008-03-29 23:11 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
There is a GNU tool called csplit that can implement complex text splitting. You can look for it.
Floor 10 Posted 2008-03-30 01:13 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
The merging code of abcd is also good, but when there are many files, the running speed is much slower than that of balinger's code.

As for the splitting tool of Csplit, the application is not convenient. I just want to use common batch commands to realize the splitting of files. As for those relying on other third-party tools, because they are not commonly used in use, so I haven't considered them...

I wonder if it can be realized by common batch commands or VBSCRIPT batch processing?

[ Last edited by zzhh612 on 2008-3-30 at 01:19 AM ]
一个专搜注册信息的链接注册信息搜索--历史是一个旋转的车轮,它的轨迹总是那样的相似回复后可见
Floor 11 Posted 2008-03-30 01:54 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
VBSCRIPT has strong character processing capabilities... Of course it can... Split
Floor 12 Posted 2008-03-30 03:07 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Using P to handle special characters is not great

Merged file:

@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do (
echo.########%%~nxi########
more "%%i"
)>>合并.txt
pause


Split file:

@echo off
for /f "tokens=1,2* delims=:#" %%i in ('more "合并.txt"^|findstr /n "########.*########"') do call :lp %%i "%%j"
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "合并.txt"') do (
if %%a gtr %n% >>%x% echo/%%b
)
pause
goto :eof
:lp
for /f "tokens=1,2* delims=:#" %%i in ('more "合并.txt"^|findstr /n "########.*########"') do if %%i gtr %1 set n=%%i&goto lp1
:lp1
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "合并.txt"') do (
if %%a gtr %1 if %%a lss %n% >>%~2 echo/%%b
)
set x=%~2
goto :eof
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
hjkk123 +2 2008-05-17 21:43
简单!简单!再简单!
Floor 13 Posted 2008-03-30 12:51 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
Terse's code is really good, which matches my original intention quite well. I tried it with 100 text files, and so far, I haven't found any errors.

It seems I still need to learn from you. I have ideas but lack the skills, haha..
一个专搜注册信息的链接注册信息搜索--历史是一个旋转的车轮,它的轨迹总是那样的相似回复后可见
Floor 14 Posted 2008-05-18 11:41 ·  中国 河南 周口 联通
初级用户
Credits 35
Posts 19
Joined 2008-05-14 19:22
18-year member
UID 118680
Gender Male
Status Offline
Support
Floor 15 Posted 2008-05-18 23:12 ·  中国 北京 联通数据上网公共出口
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
find /V "" c:\*.txt

Please use the following instead:
Forum Jump: