windows 将文件中的数据附加到 .bat 文件中的另一个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5137031/
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
Appending data from a file to another file in a .bat file
提问by res1
I have a .bat file that generates a file from some previous commands, i need to add to this .bat file some dos commands that append the content of this file to another file, the names of the sourcefile and of the targetfile are fixed and both are text files.
我有一个 .bat 文件,它从以前的一些命令生成一个文件,我需要向这个 .bat 文件添加一些 dos 命令,这些命令将这个文件的内容附加到另一个文件中,源文件和目标文件的名称是固定的,并且两者都是文本文件。
There can be cases where the source file is not created from the commands on the .bat so maybe it is possible to add a check on this condition before executing the append command?
在某些情况下,源文件不是从 .bat 上的命令创建的,所以也许可以在执行 append 命令之前添加对这种情况的检查?
How can i do this?
我怎样才能做到这一点?
I tried copy target+source target
but sometimes using this i find the target file with some extra characters at line start, i don't know why.
我试过,copy target+source target
但有时使用它我发现目标文件在行开头有一些额外的字符,我不知道为什么。
Thanks
谢谢
采纳答案by j_random_hacker
You cannot directly copy on top of the original file(s). Also make sure you use the /B
switch for COPY
to copy using "binary mode" -- failing to do this has the following negative consequences:
您不能直接复制到原始文件的顶部。还要确保您使用/B
开关COPY
使用“二进制模式”进行复制——不这样做会产生以下负面后果:
- Any "end-of-file" character (ASCII code 26) appearing in one of the source files will prematurely truncate the file at that point. Text files don't usually contain this character, but binary files (e.g. .EXE files or .DOC files) often do.
- An "end-of-file" character will be appended to the end of the output file.
- 出现在源文件之一中的任何“文件结尾”字符(ASCII 代码 26)将在该点过早地截断文件。文本文件通常不包含此字符,但二进制文件(例如 .EXE 文件或 .DOC 文件)通常包含此字符。
- “文件结束”字符将附加到输出文件的末尾。
Example of how to do it right:
如何正确执行的示例:
copy /B input1 + input2 output
move /Y output input1
The move
command moves the file output
back on top of input1
; /Y
suppresses the "Overwrite?" prompt you would otherwise see.
该move
命令将文件output
移回input1
; /Y
抑制“覆盖?” 提示你否则会看到。
回答by Bladean Mericle
If your text was encoded UTF-8 or UTF-16, Maybe extra characters is BOM(Byte Order Mark)?
BOM exists start of file and has 3 byte length.
Is it match your probrem?
如果您的文本是 UTF-8 或 UTF-16 编码的,也许额外的字符是 BOM(字节顺序标记)?
BOM 存在于文件开头,长度为 3 个字节。
它符合您的问题吗?