简体   繁体   English

如何合并所有子目录中具有相同名称的文本文件?

[英]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.

相关问题 Windows批处理-尝试合并所有子目录中具有相同名称的文本文件时出现空文件 - Windows Batch - empty files while trying to merge text files with the same name from all subdirectories 如何使用 CMD 将所有具有特定扩展名的文件从所有子目录移动到其父目录? - How to move all files with specific extension from all subdirectories to their parent using CMD? 在所有子目录中显示具有相同文件名的多个 .txt 文件的内容 - Displaying content of multiple .txt files with the same filename in all subdirectories Windows 驱动程序内核:如何枚举所有子目录和文件? - Windows driver kernel: How enumerate all subdirectories and files? 如何删除目录和子目录中除某些文件夹之外的所有文件? - How to delete all files in a directory and subdirectories except for certain folders? Windows打开文件对话框,其中包含所有文件+子目录 - Windows open files dialog with all files + subdirectories Windows批处理文件 - 连接子目录中的所有文件 - Windows batch file - Concatenate all files in subdirectories 循环浏览子目录中的文件夹并合并文本文件 - Loop through folders in subdirectories and combine text files 将文件夹结构和子目录/文件导出到文本 - Exporting Folder Structure & Subdirectories/files to text 打印给定路径中的所有文件和子目录 - print all files and subdirectories in a give path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM