|
zzhh612
中级用户
  
积分 278
发帖 103
注册 2006-10-21
状态 离线
|
『楼 主』:
如何按行读取多个文本文件来实现文本的合并
使用 LLM 解释/回答一下
按行读取多个文本文本文件到一新的文件中(也即文本文件的合并)(要求能原样输出文件中的空行及前导和行尾空白),本人写了一个但空行被忽略了,而遇到含空格或制表符等的空白行时,则会向文件写入"echo 已经关闭"不知如何能解决这些问题?
@echo off
setlocal EnableDelayedExpansion %=启用延迟变量=%
dir/b/o:n *.txt>filelist.tmp %=得到文件列表=%
type.>HBfile.txt %=先建立结果文件=%
for /f "usebackq delims=" %%a in ("filelist.tmp") do ( %=得到一个文件名=%
set filename=%%a %=将文件名赋于变量=%
call :sub !filename! %=带参调用子过程=%
)
del filelist.tmp %=完成后删除临时文件=%
goto :eof %=退出=%
:sub
for /f "usebackq delims=" %%i in ("%1") do (
echo %%i>>HBfile.txt) %=将得到的内容写入结果文件=%
echo.>>HBfile.txt %=在每一个文件完成后加入一空行=%
goto :eof %=退出子过程=%
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=%
|

一个专搜注册信息的链接回复后可见 |
|
2008-3-29 20:17 |
|
|
beeny
初级用户
 
积分 136
发帖 49
注册 2007-1-4
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
用copy HBfile.txt+newfile.txt HBfile.txt
Use the command "copy HBfile.txt+newfile.txt HBfile.txt"
|
|
2008-3-29 20:41 |
|
|
bat-zw
金牌会员
      永远的学习者
积分 3105
发帖 1276
注册 2008-3-8
状态 离线
|
『第 3 楼』:
问题没描述清楚:
使用 LLM 解释/回答一下
我想:
1 可以用copy copy a.txt +b.txt+c.txt+...........+n.txt m.txt
2 使用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 ]
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 |
|
2008-3-29 20:46 |
|
|
balinger
中级用户
  
积分 356
发帖 115
注册 2004-7-27
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do (more "%%i" >>合并.txt)
pause
@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do (more "%%i" >>合并.txt)
pause
|
|
2008-3-29 20:56 |
|
|
zzhh612
中级用户
  
积分 278
发帖 103
注册 2006-10-21
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
谢谢各位,是我把问题想复杂了,
我的本意是要建立一个文本文件合并与分割的批处理,在合并时将每个文件之间加入一个分隔标志的行,以便于在以后分割时作为标记.这个标志例如:
-----------------*.TXT-------------------
以后在分割时,先从中提取出文件名,然后将下一部分内容写入到这个文件中,从而完成分割出一个文件.这样多个文件在经过合并再分割后,能完全恢复到原来的状态.
不过这个批处理我还没有写出来....
因为分割文件似乎要难一些了?
Last edited by zzhh612 on 2008-3-29 at 10:37 PM ]
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 ]
|

一个专搜注册信息的链接回复后可见 |
|
2008-3-29 22:32 |
|
|
balinger
中级用户
  
积分 356
发帖 115
注册 2004-7-27
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do (
echo -----------------%%~nxi-------------------
more "%%i"
echo -----------------%%~nxi-------------------
)>>合并.txt
@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do (
echo -----------------%%~nxi-------------------
more "%%i"
echo -----------------%%~nxi-------------------
)>>Merge.txt
|
|
2008-3-29 22:45 |
|
|
zzhh612
中级用户
  
积分 278
发帖 103
注册 2006-10-21
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
谢谢balinger的代码.
你的代码符符合我的意思,分割开来我却搞不定
主要是从中提取出文件名,并在两个标记间提取出文本,这一定要按行读取了,
这又涉及到了空行和空白行的问题了
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.
|

一个专搜注册信息的链接回复后可见 |
|
2008-3-29 22:53 |
|
|
zerocq
中级用户
  
积分 458
发帖 196
注册 2006-10-5
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
# 显示两个正则表达式之间的文本(包含)
sed -n '/Iowa/,/Montana/p'
这应该可以提取文件名之间的内容吧
### Display text between two regular expressions (inclusive)
sed -n '/Iowa/,/Montana/p'
This should be able to extract the content between filenames, right?
|
|
2008-3-29 22:57 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
有一个gnu工具叫csplit可以实现复杂的文本分割
你可以找找看
There is a GNU tool called csplit that can implement complex text splitting. You can look for it.
|
|
2008-3-29 23:11 |
|
|
zzhh612
中级用户
  
积分 278
发帖 103
注册 2006-10-21
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
abcd的合并代码也不错,只是文件多时运行速度较balinger的代码要慢许多.
至于Csplit的分割工具,应用并不方便,我只是想用常用的批处理命令来
实现文件的分割,至于借助于其它第三方工具的,因为在使用上不太通行,
所以也没有去考虑了...
不知能否以通常的批处理命令或是VBSCRIPT批处理来实现分割呢?
Last edited by zzhh612 on 2008-3-30 at 01:19 AM ]
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 ]
|

一个专搜注册信息的链接回复后可见 |
|
2008-3-30 01:13 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
VBSCRIPT的字符处理能力很强。。。当然可以。。。分割
VBSCRIPT has strong character processing capabilities... Of course it can... Split
|
|
2008-3-30 01:54 |
|
|
terse
银牌会员
    
积分 2404
发帖 946
注册 2005-9-8
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
用P处理特殊字符不怎么样
合并的文件:
@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do (
echo.########%%~nxi########
more "%%i"
)>>合并.txt
pause
分割文件:
@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
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
|

简单!简单!再简单! |
|
2008-3-30 03:07 |
|
|
zzhh612
中级用户
  
积分 278
发帖 103
注册 2006-10-21
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Terse的代码真是不错,比较符合我的本意,我用了100个文本文件试了一下,目前还没有发现什么错误.
看来我还要向你学习了,我是有思路,没技术,哈哈..
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..
|

一个专搜注册信息的链接回复后可见 |
|
2008-3-30 12:51 |
|
|
taqiao
初级用户
 
积分 35
发帖 19
注册 2008-5-14
状态 离线
|
|
2008-5-18 11:41 |
|
|
knoppix7
银牌会员
    
积分 1287
发帖 634
注册 2007-5-2 来自 cmd.exe
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
find /V "" c:\*.txt
代用以下把
find /V "" c:\*.txt
Please use the following instead:
|
|
2008-5-18 23:12 |
|