A command-line or batch cmd to concatenate multiple files

100

46

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.

Mirage

Posted 2010-02-22T01:59:20.953

Reputation: 2 893

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. – None – 2010-02-22T02:06:50.520

The post is edited – Mirage – 2010-02-22T02:18:19.753

2DOS 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 – 2010-02-22T02:21:03.883

sorry for that , i really didn't knew that. I was thinking as DOS – Mirage – 2010-02-22T02:30:03.133

1thankfully, 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 – 2010-02-22T02:32:18.793

@~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 – 2010-02-22T11:06:19.563

Answers

129

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

Molly7244

Posted 2010-02-22T01:59:20.953

Reputation:

Is it possible to insert new line character after everyfile – Mirage – 2010-02-22T02:32:41.367

not with this method. – quack quixote – 2010-02-22T02:37:21.267

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 – 2010-02-22T02:42:30.837

@Mirage - updated my answer according to your comment. – None – 2010-02-22T02:50:55.360

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 – 2010-02-22T03:11:09.267

Link moved to here: http://www.rondebruin.nl/win/s3/win021.htm

– Ryan R – 2013-05-29T14:54:24.637

For what it is worth -> copy *.txt merged.txt is a lot less painful (Windows 7). – Daniel Chapman – 2013-12-04T16:56:06.850

38

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

type *.txt > newfile.txt

Echeban

Posted 2010-02-22T01:59:20.953

Reputation: 511

1Wow, type has come a long way since DOS 3.3. I did not know you can use file masks. When did that happen? – Sun – 2016-05-31T21:24:10.130

5WARNING: When you use **type .txt > newfile.txt*, the text is duplicated. – Malganis – 2013-07-19T12:37:07.553

3Remove .txt from newfile and bam! There you have it. – fa wildchild – 2013-08-29T03:48:07.133

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 – 2013-12-04T16:49:30.637

32

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.

nik

Posted 2010-02-22T01:59:20.953

Reputation: 50 788

Microsoft docs on copy https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy

– S Meaden – 2019-07-27T19:41:31.600

5This is quite useful if you need to concatenate files in a particular order. – kapex – 2012-02-24T02:50:40.827

8

Run the following command in the command prompt:

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

Abhishek Goel

Posted 2010-02-22T01:59:20.953

Reputation: 201

1That does not work as expected, all the text is duplicated in output.txt – DavidPostill – 2015-03-25T20:46:31.177

3Hint *.txt matches output.txt – DavidPostill – 2015-03-25T20:51:00.200

@DavidPostill, I've edited the answer according to your concern. – Saran – 2017-01-11T11:10:06.457

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

Posted 2010-02-22T01:59:20.953

Reputation: 511

2A 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 – 2014-06-26T11:25:13.593

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  – 2016-03-15T22:01:53.390

I am aware that using a bash shell would probably make more sense! – Curtis Price – 2016-03-15T22:05:04.313