![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-28 07:40 |
47,813 topics / 349,918 posts / today 0 new / 48,277 members |
| DOS批处理 & 脚本技术(批处理室) » How to read multiple text files line by line to achieve text merging |
| Printable Version 2,594 / 15 |
| Floor1 zzhh612 | Posted 2008-03-29 20:17 |
| 中级用户 Posts 103 Credits 278 | |
|
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=% |
|
| Floor2 beeny | Posted 2008-03-29 20:41 |
| 初级用户 Posts 49 Credits 136 | |
|
Use the command "copy HBfile.txt+newfile.txt HBfile.txt"
|
|
| Floor3 bat-zw | Posted 2008-03-29 20:46 |
| 金牌会员 Posts 1,276 Credits 3,105 | |
|
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 ] |
|
| Floor4 balinger | Posted 2008-03-29 20:56 |
| 中级用户 Posts 115 Credits 356 | |
|
@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do (more "%%i" >>合并.txt) pause |
|
| Floor5 zzhh612 | Posted 2008-03-29 22:32 |
| 中级用户 Posts 103 Credits 278 | |
|
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 ] |
|
| Floor6 balinger | Posted 2008-03-29 22:45 |
| 中级用户 Posts 115 Credits 356 | |
|
@echo off
for /f "delims=" %%i in ('dir /s/b/a-d *.txt') do ( echo -----------------%%~nxi------------------- more "%%i" echo -----------------%%~nxi------------------- )>>Merge.txt |
|
| Floor7 zzhh612 | Posted 2008-03-29 22:53 |
| 中级用户 Posts 103 Credits 278 | |
|
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. |
|
| Floor8 zerocq | Posted 2008-03-29 22:57 |
| 中级用户 Posts 196 Credits 458 | |
|
### Display text between two regular expressions (inclusive)
sed -n '/Iowa/,/Montana/p' This should be able to extract the content between filenames, right? |
|
| Floor9 qzwqzw | Posted 2008-03-29 23:11 |
| 银牌会员 Posts 636 Credits 2,343 | |
|
There is a GNU tool called csplit that can implement complex text splitting. You can look for it.
|
|
| Floor10 zzhh612 | Posted 2008-03-30 01:13 |
| 中级用户 Posts 103 Credits 278 | |
|
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 ] |
|
| Floor11 slore | Posted 2008-03-30 01:54 |
| 铂金会员 Posts 2,478 Credits 5,212 | |
|
VBSCRIPT has strong character processing capabilities... Of course it can... Split
|
|
| Floor12 terse | Posted 2008-03-30 03:07 |
| 银牌会员 Posts 946 Credits 2,404 | |
|
Using P to handle special characters is not great
Merged file: Split file: |
|
| Floor13 zzhh612 | Posted 2008-03-30 12:51 |
| 中级用户 Posts 103 Credits 278 | |
|
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.. |
|
| Floor14 taqiao | Posted 2008-05-18 11:41 |
| 初级用户 Posts 19 Credits 35 | |
|
Support
|
|
| Floor15 knoppix7 | Posted 2008-05-18 23:12 |
| 银牌会员 Posts 634 Credits 1,287 From cmd.exe | |
|
find /V "" c:\*.txt
Please use the following instead: |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |