简体   繁体   中英

How do I append to a file using the COPY command

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

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

怎么样type file2 >> file1

make sure you start with fresh files you never tried to copy over.
I just found that on my (XP sp3) copy a+ba without /b appends 1A ( SUB ) to the end of the file which makes anything after it disappear from output of type (but more will show it). Copy /b a+ba works.

@echo off

cls

type "file2.txt" >> "file1.txt"

exit

Answer to: " How do I append to a file using the COPY command "

WARNING : If you have a list of files you wish to combine via COPY command, it's simple but can potentially destroy your files.

Dangerous Way :

copy /b one + two + three -- will append contents of "two" and "three" to the file "one" . So the original "one" now has contents of 3 files in proper sequence. If during copy-process things go wrong, you'll have no way of recovering original "one", as it will be corrupt and your data would essentially be lost. There's almost Never a reason to use this way.

Safe Way :

copy /b one + two new_filename -- will combine 2 files (you can list more than two of course), creating a new_filename containing "one" and "two" in proper sequence, and leaving original files intact.

你有没有尝试copy /b file1 + file2 file1

copy /b input1 + input2 output
del input1
ren output input1
Maybe this? :P

C:\Users\Nikos>type file1
foo
bar

C:\Users\Nikos>copy file1+con=file1
file1
con
ihdui
ohisd
^Z
        1 file(s) copied.

C:\Users\Nikos>type file1
foo
bar
ihdui
ohisd

Wait! There is more! (Win7 Pro)

>ver

Microsoft Windows [Version 6.1.7601]

>copy file.a.txt + file.b.txt file.ab.txt
file.a.txt
1 file(s) copied

>copy fileA.txt + fileB.txt fileAB.txt
fileA.txt
fileB.txt
1 files(s) copied

Putting aside the "1 file(s) copied" notification, it just doesn't like files with funny names.

To copy to a binary file ...

Put all files in same folder eg G:\\Files

g:
cd Files
copy /B *.* TargetFilename.ext

and .ext is the type of file required

        echo . 2>new.mp3
        rem create a new empty.
        for /f "delims=" %%i in ('dir /b *.mp3') do copy /b "%cd%\new.mp3"+"%cd%\%%i.mp3" %cd%\new.mp3
        
        rem loop merge mp3 to new.mp3
        rem or you can edit a list to merge files
        for /f "delims=" %%i in (list.txt) do copy /b "%cd%\new.mp3"+"%cd%\%%i.mp3" %cd%\new.mp3
  1. Put all the files in the same folder.
  2. copy *.* ´target.ext'
  3. ????
  4. Profit!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM