119

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.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Mirage
  • 3,153
  • 14
  • 45
  • 54
  • 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. –  Feb 22 '10 at 02:06
  • The post is edited – Mirage Feb 22 '10 at 02: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 02:21
  • sorry for that , i really didn't knew that. I was thinking as DOS – Mirage Feb 22 '10 at 02: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 02:32
  • @~quack: For Windows batch questions it *is* wrong, as the answer is in many cases quite different (due to the actual DOS command line environment being quite inferior). – Joey Feb 22 '10 at 11:06

6 Answers6

152

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

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
  • Is it possible to insert new line character after everyfile – Mirage Feb 22 '10 at 02:32
  • not with this method. – quack quixote Feb 22 '10 at 02: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 02:42
  • @Mirage - updated my answer according to your comment. –  Feb 22 '10 at 02: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 03:11
  • Link moved to here: http://www.rondebruin.nl/win/s3/win021.htm – Ryan R May 29 '13 at 14:54
  • For what it is worth -> `copy *.txt merged.txt` is a lot less painful (Windows 7). – Daniel Chapman Dec 04 '13 at 16:56
  • I wonder: Is there any defined order when using such a command? Imagine the files are part of an ISO, so the order may be important. – U. Windl Nov 23 '21 at 10:41
47

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

type *.txt > newfile.txt
slhck
  • 223,558
  • 70
  • 607
  • 592
Echeban
  • 600
  • 4
  • 5
  • 7
    WARNING: When you use **type *.txt > newfile.txt**, the text is duplicated. – Malganis Jul 19 '13 at 12:37
  • 6
    Remove `.txt` from `newfile` and bam! There you have it. – fa wildchild Aug 29 '13 at 03: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 04 '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
38

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.

wfaulk
  • 6,200
  • 5
  • 34
  • 45
nik
  • 55,788
  • 10
  • 98
  • 140
10

Run the following command in the command prompt:

for %f in (*.txt) do type "%f" >> output.txt
Halil Sen
  • 126
  • 6
Abhishek Goel
  • 231
  • 2
  • 4
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

:)

Echeban
  • 600
  • 4
  • 5
  • 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

Zimba
  • 1,051
  • 11
  • 15