![](/img/trans.png)
[英]Windows Batch - empty files while trying to merge text files with the same name from all subdirectories
[英]How to merge text files with the same name from all subdirectories?
我有几个带文件的文件夹。 文件可以具有相同的名称。 我想将文件串联成每个名称之一。 提前致谢。
编辑:对不起,您能给我看看它的批处理文件吗?
这很简单-您必须将每个文件都写入同一目录,以确保要追加。 这不能保证任何顺序的首选项,因此我认为这是无关紧要的。 您尚未指定语言或是否为外壳指定语言,因此我还不能建议实现。
此问题可以分解为以下任务。 首先,您需要所有文件的列表。 如果您正在“编程”,则可以使用ls -r
或某些PL特定的方式来完成。 然后,您需要找出每个文件路径的写入位置,其中涉及一个正则表达式,甚至可能是对“ /”的分割。 然后,您只需要读取A到B的每个文件并将其附加到B即可。 可以使用cat a >> b
或使用的任何语言库来实现。
合并
@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
@echo off
for /r "." %%a in (*) do (
type %%a >> all_merged.txt
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.