-3

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.

USLG
  • 25
  • 4
  • 2
    Asking volunteers to write code for you, for free, does not go over well here. Please *edit* your question to show us how you've tried to solve the problem yourself, with code. – William Price Dec 15 '14 at 05:26
  • I have tried... I can get it to work within one directory using: copy /a *NAD.TXT COMBINED_NAD.TXT... but can't get it to work recursing subdirectories individually. Below is what I tried: - for %f in (`dir /b /s *NAD.TXT`) do type “%f” >> COMBINED_NAD.TXT // AND // for /F “usebackq delims==” %f in (`dir /b /s *NAD.TXT`) do type “%f” >> COMBINED_NAD.TXT - neither worked as I need – USLG Dec 15 '14 at 05:51
  • Thanks for the clarification. Please, edit your question to add the information you have written in the comment. – PA. Dec 15 '14 at 08:45

1 Answers1

2
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

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • That works perfectly. Thank you. IS there any way to add the directory name to the start of the file it creates - for example - "1_COMBINED_NAD.TXT" where the directory the files are created within is "1"? – USLG Dec 15 '14 at 17:37
  • @USLG, as `%%a` is holding a reference to the folder being processed, `%%~nxa` will return the name (with extension if present) of the folder. Change the file deletion and rename lines: `del "%%~fa\%%~nxa_COMBINED_NAD.TXT"`, `ren "%%~fa\COMBINED_NAD.TMP" "%%~nxa_COMBINED_NAD.TXT"` – MC ND Dec 15 '14 at 21:43