How to merge text files with the same name from all subdirectories?

后端 未结 2 961
庸人自扰
庸人自扰 2021-01-29 02:23

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

相关标签:
2条回答
  • 2021-01-29 02:55

    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". 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. Either do that with cat a >> b or with whatever language libraries you are using.

    0 讨论(0)
  • 2021-01-29 03:02

    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

    @echo off
    for /r "." %%a in (*) do (
        type %%a >> all_merged.txt
    )
    
    0 讨论(0)
提交回复
热议问题