How do I append to a file using the COPY command

James K picture James K · Sep 9, 2012 · Viewed 86.1k times · Source

I'm running Windows 7 Ultimate x64, but my experience dates back to DOS 3.0.

Since like DOS 3.1 you've been able to append a file to another one with this use of the COPY command:

COPY FILE1+FILE2=FILE1

Making the need for a temporary FILE3 unnecessary.

It was a very convenient command since whenever you added a new program you often needed to update your CONFIG.SYS and AUTOEXEC.BAT files.

It also used to be that getting the order correct was importiant, otherwise you'd end up with an empty FILE1.

But today when I tried that, it left FILE1 untouched, and when I reversed the order, it (understandably) made FILE1 a copy of FILE2.

Does anyone know if it's been replaced with another method, and when this change happened?

EDIT:

I've been doing more testing, and oddly even though the above code won't work, you still can sill copy from the console and append that to an existing file like this:

copy file1+con=file1
Type some text to append to file1
^Z ([CTRL]+Z the End Of File character)

I'm wondering if my version of Windows is messed up somehow. Can any body replicate my findings?

EDIT:

It works on 95 / 98 / ME / 2000 / XP / XP Mode / 7 Professional x64 / 8 x64. So I imagine that it's not a 7 Ultimate x64 problem, but rather an issue with my machine.

* Sigh *

EDIT:

Last edit, I promise. :)

It was not an issue with my machine, it was an issue with File1. Apparently when I first appended File2 to it, the [CTRL]+Z (EOF character) never got overwritten, causing the file to look like this:

Original Data
Original Data
[EOF]
Appended Data
Appended Data
Appended Data

You can duplicate this yourself with the following experiment from at the command prompt. (Where ^Z is the character [CTRL]+Z )

At the command prompt type:

copy con file1
File One
^Z^Z

copy con file2
File Two
^Z

copy con file3
File Three
^Z

copy file1+file2=file1

copy file2+file3=file2

TYPE file1
TYPE file2

You will see:

file1

File One

file2

File Two
File Three

You can type file2 >> file1 or use nearly any other method of concatenating files, and when you type file1 it will still only appear to contain File One. BUT if you use FIND "searchterm" file to parse the file it will show you what's REALLY going on. In this case type:

FIND " " file1

And you will be rewarded with:

---------- FILE1
File One
→File Two

Answer

nikos1993pl picture nikos1993pl · Sep 9, 2012

Windows 8 x86:

Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.

C:\Users\Nikos>echo foo>file1

C:\Users\Nikos>echo bar>file2

C:\Users\Nikos>copy /b file1 + file2 file1
file1
file2
        1 file(s) copied.

C:\Users\Nikos>type file1
foo
bar