COPY 将至少一个文件复制到另一个位置
C:\Users\Administrator>copy /?
将一份或多份文件复制到另一个位置。
COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
[+ source [/A | /B] [+ ...]] [destination [/A | /B]]
source 指定要复制的文件。
/A 表示一个 ASCII 文本文件。
/B 表示一个二进位文件。
/D 允许解密要创建的目标文件
destination 为新文件指定目录和/或文件名。
/V 验证新文件写入是否正确。
/N 复制带有非 8dot3 名称的文件时,尽可能使用短文件名。
/Y 不使用确认是否要覆盖现有目标文件的提示。
/-Y 使用确认是否要覆盖现有目标文件的提示。
/Z 用可重新启动模式复制已联网的文件。
/L 如果源是符号链接,请将链接复制到目标而不是源链接指向的实际文件。
命令行开关 /Y 可以在 COPYCMD 环境变量中预先设定。
这可能会被命令行上的 /-Y 替代。
除非 COPY 命令是在一个批处理脚本中执行的,默认值应为在覆盖时进行提示。
要附加文件,请为目标指定一个文件,为源指定数个文件(用通配符或 file1+file2+file3 格式)。(1)将当前目录下的 test.txt 文件拷贝到 bak 目录,如下:
C:\Users\Administrator\Desktop\tmp>copy test.txt bak
已复制 1 个文件。
C:\Users\Administrator\Desktop\tmp>tree /F /A
卷 Windows10 的文件夹 PATH 列表
卷序列号为 7A18-2861
C:.
| test.txt
|
\---bak
test.txt(2)将当前目录下的 test.txt 文件拷贝到 bak 目录,且重命名为 myTest.txt。如下:
C:\Users\Administrator\Desktop\tmp>copy test.txt bak\myTest.txt
已复制 1 个文件。
C:\Users\Administrator\Desktop\tmp>tree /F /A
卷 Windows10 的文件夹 PATH 列表
卷序列号为 7A18-2861
C:.
| test.txt
|
\---bak
myTest.txt
test.txt(3)将当前目录下的 test.txt 文件拷贝到 D 盘,如下:
C:\Users\Administrator\Desktop\tmp>copy test.txt D:\
已复制 1 个文件。(4)将 C 盘下的 test.txt 文件拷贝到 D 盘,如下:
C:\Users\Administrator\Desktop\tmp>copy c:\test.txt d:\
覆盖 d:\test.txt 吗? (Yes/No/All): yes
已复制 1 个文件。注意:如果在 D 盘下面已经存在 test.txt 文件,则提示是否覆盖。
(1)将当前目录下的 src 目录中所有文件复制到 target 目录中,如下:
C:\Users\Administrator\Desktop\tmp>copy src target
src\test1.txt
src\test2.txt
已复制 2 个文件。
C:\Users\Administrator\Desktop\tmp>tree /F /A
卷 Windows10 的文件夹 PATH 列表
卷序列号为 7A18-2861
C:.
+---src
| test1.txt
| test2.txt
|
\---target
test1.txt
test2.txt(2)将 src 目录中所有 txt 格式的文件复制到 target 目录中,如下:
C:\Users\Administrator\Desktop\tmp>copy src\*.txt target /y
src\test1.txt
src\test2.txt
已复制 2 个文件。
C:\Users\Administrator\Desktop\tmp>tree /F /A
卷 Windows10 的文件夹 PATH 列表
卷序列号为 7A18-2861
C:.
+---src
| hxstrive.xlsx
| test1.txt
| test2.txt
|
\---target
test1.txt
test2.txt上面 *.txt 表示所有 txt 文件。当 target 目录中已经存在了和 src 目录中相同文件名的文件时,出现 “覆盖*吗?(Yes/No/All):” 提示,此时可通过参数 /y 实现文件无须确认直接覆盖。例如:
C:\Users\Administrator\Desktop\tmp>copy src\*.txt target
src\test1.txt
覆盖 target\test1.txt 吗? (Yes/No/All): All
src\test2.txt
已复制 2 个文件。
C:\Users\Administrator\Desktop\tmp>copy src\*.txt target /y
src\test1.txt
src\test2.txt
已复制 2 个文件。(1)把当前目录下的 test1.txt 和 test2.txt 文件内容进行合并,如下:
C:\Users\Administrator\Desktop\tmp>type test1.txt
world
C:\Users\Administrator\Desktop\tmp>type test2.txt
hello
C:\Users\Administrator\Desktop\tmp>copy test1.txt+test2.txt join.txt
test1.txt
test2.txt
已复制 1 个文件。
C:\Users\Administrator\Desktop\tmp>type join.txt
worldhello注意:根据网上资料显示,可以使用 copy /b 合并二进制文件,如:MP3、MP4等,但笔者亲自试了一下,复制成功后,目标文件仅有第一个文件的内容,原则上合并失败。
合成图片/歌曲这样的二进制文件必须使用/b参数(b代表Binaty,二进制),否则将会失败;另一个合并参数是/a(ASCll,文本文件),只能用于纯文本的合并。两参数不能同时使用,二进制方式可以合并文本文件和二进制文件,而文本方式用于纯文本的合并。