How do I append to a file using the COPY command

我与影子孤独终老i 提交于 2020-01-03 06:48:07

问题


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

回答1:


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



回答2:


What about type file2 >> file1




回答3:


make sure you start with fresh files you never tried to copy over.
I just found that on my (XP sp3) copy a+b a 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+b a works.




回答4:


@echo off

cls

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

exit



回答5:


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.




回答6:


Did you try copy /b file1 + file2 file1




回答7:


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




回答8:


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



回答9:


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.




回答10:


To copy to a binary file ...

Put all files in same folder e.g. G:\Files

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

and .ext is the type of file required




回答11:


  1. Put all the files in the same folder.
  2. copy *.* ´target.ext'
  3. ????
  4. Profit!


来源:https://stackoverflow.com/questions/12342764/how-do-i-append-to-a-file-using-the-copy-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!