windows 如何使用 COPY 命令附加到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12342764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:52:08  来源:igfitidea点击:

How do I append to a file using the COPY command

windowsbatch-filecmdwindows-7-x64

提问by James K

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

我运行的是 Windows 7 Ultimate x64,但我的经验可以追溯到 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:

像 DOS 3.1 一样,您可以使用 COPY 命令将一个文件附加到另一个文件中:

COPY FILE1+FILE2=FILE1

Making the need for a temporary FILE3 unnecessary.

不需要临时 FILE3。

It was a very convenient command since whenever you added a new program you often needed to update your CONFIG.SYSand AUTOEXEC.BATfiles.

这是因为,只要你加入,你经常需要更新您的一个新的程序非常方便的命令CONFIG.SYSAUTOEXEC.BAT文件。

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

过去,正确的顺序也很重要,否则你最终会得到一个空的 FILE1。

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

但是今天当我尝试这样做时,它保持FILE1原样,当我颠倒顺序时,它(可以理解)FILE1复制了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?

我想知道我的 Windows 版本是否以某种方式搞砸了。任何机构都可以复制我的发现吗?

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 x64problem, but rather an issue with my machine.

它适用于 95 / 98 / ME / 2000 / XP / XP Mode / 7 Professional x64 / 8 x64。所以我想这不是7 Ultimate x64问题,而是我的机器的问题。

*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:

这不是我机器的问题,而是 File1 的问题。显然,当我第一次将 File2 附加到它时,[CTRL]+Z(EOF 字符)从未被覆盖,导致文件如下所示:

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 )

您可以在命令提示符下使用以下实验自行复制此内容。(其中 ^Z 是字符 [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

file1

File One

file2

file2

File Two
File Three

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

您可以type file2 >> file1或使用几乎任何其他连接文件的方法,并且当您键入时,file1它仍然只会显示包含File One. 但是如果你FIND "searchterm" file用来解析文件,它会告诉你到底发生了什么。在这种情况下键入:

FIND " " file1

And you will be rewarded with:

您将获得以下奖励:

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

回答by nikos1993pl

Windows 8 x86:

视窗 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

回答by nikos1993pl

What about type file2 >> file1

关于什么 type file2 >> file1

回答by wmz

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

确保从从未尝试复制的新文件开始。
我刚刚发现在我的 (XP sp3) 上copy a+b a没有/b1A( SUB)附加到文件末尾,这使得它从输出中消失后的任何内容type(但more会显示它)。Copy /b a+b a作品。

回答by keval

@echo off

cls

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

exit

回答by Emil

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

回答:“如何使用 COPY 命令附加到文件中

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

警告:如果您有一个希望通过 COPY 命令组合的文件列表,这很简单,但可能会破坏您的文件。

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.

copy /b one + two + three-- 将“二”和“三”的内容附加到文件“一”中。所以原来的“一个”现在有 3 个文件的内容,按正确的顺序排列。如果在复制过程中出现问题,您将无法恢复原始“一个”,因为它会损坏并且您的数据基本上会丢失。几乎没有理由使用这种方式。

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 one + two new_filename-- 将合并 2 个文件(当然您可以列出两个以上),以适当的顺序创建一个包含“一”和“二”的 new_filename,并保持原始文件不变。

回答by Nigel

To copy to a binary file ...

要复制到二进制文件...

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

将所有文件放在同一个文件夹中,例如 G:\Files

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

and .extis the type of file required

.ext是文件所需的类型

回答by david

Wait! There is more! (Win7 Pro)

等待!还有更多!(Win7 专业版)

>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.

撇开“已复制 1 个文件”通知不谈,它只是不喜欢名称有趣的文件。

回答by nikos1993pl

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

回答by nikos1993pl

Did you try copy /b file1 + file2 file1

你试过了吗 copy /b file1 + file2 file1

回答by nikos1993pl

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

copy /b input1 + input2 output
del input1
ren output input1
也许这个?:P