122

I have 50 text files in one directory.

Is there a Windows command-line method to concatenate those files into a single file?

I am using Windows Vista.

I don't want to type the name of all files.

6
  • which version of DOS are you using? :) c'mon, give us some more info, what file types ... you're obviously looking for a way to merge those files.
    – Molly7244
    Feb 22, 2010 at 2:06
  • The post is edited
    – Mirage
    Feb 22, 2010 at 2:18
  • 2
    DOS in Windows NT-based OS's (NT, 2000, and everything since XP) really isn't DOS, it's a command shell called "cmd.exe". removed DOS tags to reflect this. Feb 22, 2010 at 2:21
  • sorry for that , i really didn't knew that. I was thinking as DOS
    – Mirage
    Feb 22, 2010 at 2:30
  • 1
    thankfully, the last vestiges of DOS died with Windows ME. :) but no worries -- most everyone still calls the Windows command-line "DOS", so it's not wrong, just inaccurate. since real DOS is still used sometimes, i'm cleaning up the DOS tag to be just real DOS questions. Feb 22, 2010 at 2:32

6 Answers 6

155

I don't want to type the name of all files.

That's easy to be avoided. Open a command prompt in this folder and type the following command:

copy /b *.txt newfile.txt

Press Enter.

Now you will have all text files in this folder ordered by date ascending merged into a single file called newfile.txt.

My ultimate aim is to store the contents of each text file in a separate column of an Excel sheet.

Here's a tutorial that may help you to achieve your "ultimate aim":

Merge all CSV or TXT files in a folder in one worksheet

8
  • Is it possible to insert new line character after everyfile
    – Mirage
    Feb 22, 2010 at 2:32
  • not with this method. Feb 22, 2010 at 2:37
  • whats the other options. my ultimate aim is to store the contents of each text file in separate column of excel sheet. ANy ideas
    – Mirage
    Feb 22, 2010 at 2:42
  • @Mirage - updated my answer according to your comment.
    – Molly7244
    Feb 22, 2010 at 2:50
  • But the problem is how can i add the endline character to each text file. Currently some of files text are in same paragrah in the merged file , so excel put it in one column. OR if i can append some endline character to all the files first and then perform merge operation
    – Mirage
    Feb 22, 2010 at 3:11
49

To add a newLine at the end of each concatenated file, use type instead of copy, as follows:

type *.txt > newfile.txt
4
  • 7
    WARNING: When you use type *.txt > newfile.txt, the text is duplicated.
    – Malganis
    Jul 19, 2013 at 12:37
  • 7
    Remove .txt from newfile and bam! There you have it. Aug 29, 2013 at 3:48
  • This is an awesome answer for concatenating log files or other things you will parse down the road. Specifically the fact you can do type x.log.* > merged.log without a batch file. New lines are pretty easy to deal with. Dec 4, 2013 at 16:49
  • 1
    Wow, type has come a long way since DOS 3.3. I did not know you can use file masks. When did that happen?
    – Sun
    May 31, 2016 at 21:24
41

Assuming you are talking about appending text files, the copy command can be used to append them together:

copy file1+file2+file3 targetfile

If you have many files, you could loop by appending one file at a time.

For binary files, add in the '/b' option:

copy /b file1+file2+file3 targetfile

This assumes that you know the binary files you are working with can be appended back-to-back; if not, you will get a lump of useless data.

3
10

Run the following command in the command prompt:

for %f in (*.txt) do type "%f" >> output.txt
3
  • 2
    That does not work as expected, all the text is duplicated in output.txt
    – DavidPostill
    Mar 25, 2015 at 20:46
  • 6
    Hint *.txt matches output.txt
    – DavidPostill
    Mar 25, 2015 at 20:51
  • @DavidPostill, I've edited the answer according to your concern.
    – Saran
    Jan 11, 2017 at 11:10
3

The following .bat file will append all *.for files, except the one named XIT.for, to a blank file named MASTER.for

type NUL > MASTER.for
FOR %%G IN (*.for) DO IF NOT "%%G" == "XIT.for" copy /A MASTER.for+"%%G" && echo. >> MASTER.for

:)

3
  • 2
    A slight twist on the above: if one wants to make sure the files are concatenated alphabetically, one should use: FOR /F %%G IN ('dir /b /o *.for') DO IF NOT "%%G" == "XIT.for" copy /A MASTER.for+"%%G" && echo. >> MASTER.for Jun 26, 2014 at 11:25
  • I like this. Another tweak I needed today is for for a filename header to be printed into the file to separate the input files. for %f in (*.txt) do ((echo. & echo == %f == & echo. & type %f ) >> *.txt.dat ) Mar 15, 2016 at 22:01
  • I am aware that using a bash shell would probably make more sense! Mar 15, 2016 at 22:05
0
set n=50
for /l %i in (1,1,%n%) do type file%i.txt >> file.txt

Works on both binary & text files & ensures files concatenate consecutively (1-50).
Tested on Win 10 CMD

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .