Search code examples
batch-filecopyxcopyrobocopy

How to create a windows batch file that combines all *NAD.TXT recursively within each directory?


I have a group of directories, and within those directories a have some files that end in *NAD.TXT. I need to combine, or copy together, all *NAD.TXT files within each directory to a file called COMBINED_NAD.TXT in each of the directories. Basically, so it combines those files and creates it in the same directory, and then recurses to the next one. Thanks for the help.


Solution

  • for /d /r %%a in (*) do (
        del "%%~fa\COMBINED_NAD.TXT" >nul 2>nul 
        copy /b "%%~fa\*NAD.TXT" "%%~fa\COMBINED_NAD.TMP"
        ren "%%~fa\COMBINED_NAD.TMP" "COMBINED_NAD.TXT"
    )
    

    For each directory, recursively :

    • If the target file exists, remove it

    • Combine all the source files into target. .tmp extension ensures the target file will not be processed as a source file

    • Rename the target file to final name