Subscribe to How-To Geek

Recommended: Click Here to Run a Free Scan for Common PC Errors   [Sponsored Link]

Keyboard Ninja: Concatenate Multiple Text Files in Windows

You have a directory full of log files that you want to import into Excel or a database so you can do some processing on them… but there are hundreds of files… how do you make them into a single file?

image Answer: Pull out your DOS hat, open a command prompt, and then use the "for" command.

The syntax works something like this:

for <variablename> in (<directorylisting>) do <command> <variablename>

So if you wanted to append all of the *.log files in a directory, you'd use the "type" command and then pipe it into a single file using the >> operator.

The difference between >> and > is that the former appends data to the end of the file, and the latter will completely replace the file, which would be pointless for what we want to do.

So here's the command you'd run, assuming you are in the directory containing the log files.

for %f in (*.log) do type "%f" >> aggregate.txt

And yes, I actually just used this command for a project at work, which is why I'm writing up this article. =)

Random thought: What on earth would a DOS hat look like?

The Geek is the founder of How-To Geek and a geek enthusiast. When he's not coming up with great how-to articles, he's probably writing at his personal blog. This article was written on 07/2/07 and tagged with: Keyboard Ninja

Comments (12)

  1. Skeeter

    I just tried this. After all the .log files in the directory are added to the aggregate file, the entire aggregate file is then added to itself (because it is now a new file in the directory). Is there a way to keep this from happening if you would want to keep the aggregate file in the same directory that you are working in?

  2. The Geek

    Skeeter:

    That did not happen for me…. but your best bet is to output to a file with a different extension, like aggregate.txt

    That way it wouldn't be included in the list of files in the directory.

  3. The Geek

    I updated the article to reflect that as well….

  4. Bob

    You can also just use the DOS copy command:

    copy /a *.log aggregate.txt

    The /a option ensures that any ^Z's at end-of-file in the source files will not be copied to the destination file.

  5. juanattack

    Much more easier with the copy DOS command and the whole aggregate file is not added at the end (happened to me too with the for method).

  6. Nigel James

    Thanks for this. It was a lifesaver when I needed to pull together a whole bunch of sql scripts into one big script.

  7. dwalker

    Life Saver!!!!

  8. chris

    LIFESAVER almost …

    The "for %f in (*.log) do type "%f" >> aggregate.txt" technique concatenated all files but then multiplied everything by 2. There was also some issue with the last line from Report A and the first line in Report B.

    The second command, "copy /a *.log aggregate.txt" worked best.

    THANKS!

  9. Glen

    Brilliant!!! Thankyou thankyou. I was going cross-eyed manually importing all these text files into access one at a time.

  10. Nicole

    Thanks!!!! This is a complete lifesaver!! So simple and effective, I got just the right data out of bazillion log files I had to go through.

  11. Ken

    This helped me lots. Thank you! The one thing I would add is that you can also use this technique in a batch file if you just use %%f instead of %f

  12. lindsey

    brillant! thank you so much!


Leave a Comment




Leave your friendly comment here. If you have a computer help question, leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Copyright © 2006-2008 HowToGeek.com. All Rights Reserved.