105

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.

  • 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 '10 at 2:06
  • The post is edited – Mirage Feb 22 '10 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. – quack quixote Feb 22 '10 at 2:21
  • sorry for that , i really didn't knew that. I was thinking as DOS – Mirage Feb 22 '10 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. – quack quixote Feb 22 '10 at 2:32
135

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

| improve this answer | |
  • Is it possible to insert new line character after everyfile – Mirage Feb 22 '10 at 2:32
  • not with this method. – quack quixote Feb 22 '10 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 '10 at 2:42
  • @Mirage - updated my answer according to your comment. – Molly7244 Feb 22 '10 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 '10 at 3:11
39

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

type *.txt > newfile.txt
| improve this answer | |
  • 5
    WARNING: When you use type *.txt > newfile.txt, the text is duplicated. – Malganis Jul 19 '13 at 12:37
  • 3
    Remove .txt from newfile and bam! There you have it. – fa wildchild Aug 29 '13 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. – Daniel Chapman Dec 4 '13 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 '16 at 21:24
34

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.

| improve this answer | |
8

Run the following command in the command prompt:

for %f in (*.txt) do type "%f" >> output.txt
| improve this answer | |
  • 2
    That does not work as expected, all the text is duplicated in output.txt – DavidPostill Mar 25 '15 at 20:46
  • 3
    Hint *.txt matches output.txt – DavidPostill Mar 25 '15 at 20:51
  • @DavidPostill, I've edited the answer according to your concern. – Saran Jan 11 '17 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

:)

| improve this answer | |
  • 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 – Guido Domenici Jun 26 '14 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 ) – Curtis Price Mar 15 '16 at 22:01
  • I am aware that using a bash shell would probably make more sense! – Curtis Price Mar 15 '16 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

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.