Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Using the DOS copy command syntax to concatenate files:

copy file1.txt+file2.txt all.txt

I know I can do this...

copy file1.txt+file2.txt file1.txt

Is this efficient? Is it doing what I'm expecting? It works, but I want to know is it actually appending to file1.txt or is it copying file1.txt (bad), concatenating file2 and then renaming to file1.txt (which is not efficient)?

share|improve this question
    
As a side note, remember that you need to use the "/b" switch if you ever decide to use copy to cat binary files. –  Brian Knoblauch Jul 23 '10 at 18:43
    
Nobody here seems to think that the case of "huge files" is worth addressing, or giving a solution to. –  Milind R Sep 12 at 7:32

3 Answers 3

up vote 7 down vote accepted

copy is copying file1.txt and file2.txt into memory, concatenating them then writing out to file1.txt. It's not copying to a new file then renaming that file so really there's not much extra disk I/O.

You can also use type.

type file2.txt >> file1.txt

The >> operator appends text. But that will, of course, not work for binary files.

share|improve this answer
2  
it should work fine for binary files. it's just not generally useful, because most binary data formats use some form of encapsulation such that even in the combined file, only the first file will be recognized & used. if you had, say, 2 raw PCM files, this would be a fine way to concatenate them (as opposed to 2 WAV files, where another program is needed to alter headers). –  quack quixote Nov 30 '09 at 17:56
1  
I was using the term renaming very loosely. If file1 and file2 are huge files I want file2 appended to file1 WITHOUT copying any file1 data (it is too big to copy again). Tyler, you have suggested it will copy file1.txt data. Using the type command, this will actually append? Thanks. –  clsturgeon Nov 30 '09 at 18:06
    
Be warned, though that old DOS versions failed to copy a target to the target with copy source+otherfile source. It resulted in a 64 kB source file because a 64 kB buffer was used for reading and writing, and after the first flush, the source was just 64 kB :-) no joke. –  TheBlastOne Mar 9 '12 at 8:22
1  
> copy is copying … into memory, concatenating them then writing out to file1.txt. It's not copying to a new file then renaming that file so really there's not much extra disk I/O. That might be true for tiny files, but definitely not true for files of any significant size. What if the two files are huge? Do a test and you will notice that memory usage does not go up during the redirection. Therefore it is not combining them in memory. And of course it cannot combine them two the original file since it has not finished reading it, so it is using a temporary file/pipe. –  Synetech Sep 1 '12 at 23:51

Is this efficient?

Sure. However, using the the /b switch can/may increase performance by simply concatenating the bytes instead of processing the files as text. This is particularly noticeable when concatenating very large text files.

Is it doing what I'm expecting?

Usually yes, but if the file was made in Linux, Mac, or other system with differing file-/line-terminators, then it may give unexpected results. It is a good idea to use the /b switch in general, even for text files.

I want to know is it actually appending to file1.txt or is it copying file1.txt (bad), concatenating file2 and then renaming to file1.txt (which is not efficient)?

Yes, it is creating a new, temporary file, deleting the original, and renaming the temp file to the original name, but deleting and renaming take no time and unless the original file is massive, you won’t normally notice even the (redundant) copying of the original file.

share|improve this answer

If they are of the same extension you could do this -

type *.txt >> fileout.tmp.

Then rename fileout.tmp to the proper extension.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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