[英]How to merge text files with the same name from all subdirectories?
I have several folders with files. 我有几个带文件的文件夹。 Files can have the same names.
文件可以具有相同的名称。 I want to concatenate files into one of each name.
我想将文件串联成每个名称之一。 Thanks in advance.
提前致谢。
EDIT: I'm sorry, can you show me the batch file for it. 编辑:对不起,您能给我看看它的批处理文件吗?
This is simple — you have go write each file to the same directory making sure you are appending. 这很简单-您必须将每个文件都写入同一目录,以确保要追加。 This does not guarantee any sort of order preference, so I assume it's irrelevant.
这不能保证任何顺序的首选项,因此我认为这是无关紧要的。 You have not specified a language or whether this is for the shell, so I can't suggest an implementation (yet).
您尚未指定语言或是否为外壳指定语言,因此我还不能建议实现。
This problem can be broken down into the following tasks. 此问题可以分解为以下任务。 First you need a list of all the files.
首先,您需要所有文件的列表。 This can be done with
ls -r
or some PL specific way, if you are "programming". 如果您正在“编程”,则可以使用
ls -r
或某些PL特定的方式来完成。 Then you need to figure out for each file path, where to write and that involves a regex or probably even a split on "/". 然后,您需要找出每个文件路径的写入位置,其中涉及一个正则表达式,甚至可能是对“ /”的分割。 Then you just want to read and append each file from A to B and that's it.
然后,您只需要读取A到B的每个文件并将其附加到B即可。 Either do that with
cat a >> b
or with whatever language libraries you are using. 可以使用
cat a >> b
或使用的任何语言库来实现。
merge.bat 合并
@echo off
# for every text file in
# the sub-dirs of current dir
for /r "." %%a in (*.txt) do (
# filename without path and extension
echo %%~na
# read file and append it to file with
# the same name prefix in current dir
type %%a >> %%~na-merged.txt
)
merge_all_in_one.bat merge_all_in_one.bat
@echo off
for /r "." %%a in (*) do (
type %%a >> all_merged.txt
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.