Search code examples
batch-filemergeconcatenationline-breakssubdirectory

How to get line breaks between every merged file content and all paths of all txt in one txt (batch, subfolders)?


chcp 1252
for /r "C:\1" %%f in (*.txt) do type "%%f" >> echo 2> "C:\2\x.txt"

I want to overwrite x.txt if I use this batch again. 2>&1 is important to get the paths and line breaks in x.txt. I want the paths and content of all txt files (this folder, all subfolders) in an alphabetical order. It creates all content in a single file. But it don't overwrite the existing content and there are no line breaks with path (2>&1). How can I integrate it?


Solution

  • (for /r "C:\1" %%f in (*.txt) do type "%%f")>"C:\2\x.txt" 2>&1
    

    Put the whole for loop into a code block and redirect the whole thing just once. 2>&1 means "Write STDERR (Stream 2) to the same destination as STDOUT (Stream 1)"