Link to home
Start Free TrialLog in
Avatar of Gorba916
Gorba916

asked on

Merging text files together with batch commands?

I would like to be able to merge the contents of all text files in a root folder and its children folders into one output text file. Is this possible with batch commands (I'm using winxp), or is there a free app that can accomplish this? Either or is fine. Much obliged.
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Yes, you can do this in a batch file.  A sample of your data and directory structure would help, but you can use a for loop and the copy command with the /a switch -

For example:

Echo.>SingleFile.txt
FOR /f "tokens=*" %%a in ('dir /b *.txt') do copy /a SingleFile.txt+%%a SingleFile.txt
If you know the children folders and files its easy.

Example

copy   /a  \a.txt  + /a  \folder1\b.txt   +  /a  \folderb\c.txt     output.txt /a

Wildcards can be used.

If you don't know the children folders then some of the other gurus here can supply the answer.
ASKER CERTIFIED SOLUTION
Avatar of GuruGary
GuruGary
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gorba916
Gorba916

ASKER

Thanks leew! I decided I wanted to try this on a series of XML files.

And your slightly modified command:

Echo.>SingleFile.txt
FOR /f "tokens=*" %%a in ('dir /b *.xml') do copy /a SingleFile.txt+%%a SingleFile.xml

Unfortunately only the last file on the list becomes SingleFile.xml... they aren't combining. Any ideas?
Thanks GuruGary! It worked... with some modifications of course...

@echo off
setlocal
set RootFolder=D:\CFusionMX\wwwroot\GameMusicJukebox\
set OutputFile=D:\CFusionMX\wwwroot\GameMusicJukebox\Output.xml

for /f "delims=" %%a in ('dir "%RootFolder%*.xml" /b /s /a') do type "%%a" >>%OutputFile%

worked!