入门命令6-复制文件:copy

copy 意思为复制,它的作用是复制文件(注意,仅仅是复制文件,不是文件夹,).看看帮助怎么说:

C:\DOCUME~1\DOUPI>copy /?
Copies one or more files to another location.
COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B]
[+ source [/A | /B] [+ ...]] [destination [/A | /B]]
source Specifies the file or files to be copied.
/A Indicates an ASCII text file.
/B Indicates a binary file.
/D Allow the destination file to be created decrypted
destination Specifies the directory and/or filename for the new file(s).
/V Verifies that new files are written correctly.
/N Uses short filename, if available, when copying a file with a
non-8dot3 name.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want to overwrite an
existing destination file.
/Z Copies networked files in restartable mode.

重点在于红色部分,将一个或者多个文件拷贝到另一位置(目录)

 

下面具体看一下

1. 基本用法  copy  文件名 目录名[文件名]  这里文件名与目录名,即可用绝对路径也可用相对路径(这里不讲这两个路径是啥东东,自己百度就有)

1 rem 如将C盘下的test.txt复制到D盘下
2 copy c:\test.txt d:\
3 rem 目录名后面如果跟了文件名,则相当于复制时进行了重命名.
4 copy c:\test.txt d:\var.txt

 

2. 帮助里有说可以复制多个文件,是神马情况?看下例:

 1 rem mkdir是创建一个文件夹
2 C:\Documents and Settings\doupi\桌面>mkdir d:\test\
3 rem 将当前文件夹下的所有文件复制到d:\test\下, 这里是通配的用法
4 C:\Documents and Settings\doupi\桌面>copy * d:\test\
5 11-26网站方面的配置.docx
6 2.0.7.1208.rar
7 2277二期客户端(2.1版)-需求规格说明书.doc
8 2277二期客户端-规格说明书.doc
9 2277二期服务端说明-QA版.docx
10 ....
11 rem 也可以有选择性的标定后缀名
12 C:\Documents and Settings\doupi\桌面>copy *.gif d:\test\
13 ....
14 rem 如果第一个参数就是文件夹(带了\),则表示将c:\test\下的所有文件复制到d:\test\下去.
15 C:\Documents and Settings\doupi\桌面>copy c:\test\ d:\test\

 

3. 竟然可以合并文件,看下例后想到了什么?(合并txt小说放在手机里?...)

 1 C:\Documents and Settings\doupi\桌面>copy con test1.txt
2 this is test111111^Z
3 已复制 1 个文件。
4 C:\Documents and Settings\doupi\桌面>copy con test2.txt
5 this is test222222^Z
6 已复制 1 个文件。
7 rem /a参数以ascii码的形式来复制文件
8 C:\Documents and Settings\doupi\桌面>copy /a test1.txt+test2.txt d:\test.txt
9 test1.txt
10 test2.txt
11 已复制 1 个文件。
12 rem 文件内容被合并了
13 C:\Documents and Settings\doupi\桌面>type d:\test.txt
14 this is test111111this is test222222

再来一个有意思的合并,mp3文件可以直接合并,想一想,利用文件切割+copy是不是可以组合出各种手机铃声...

注意,要带参数/b  即以二进制的形式来处理文件

1 F:\>copy /b 11.mp3+22.mp3 33.mp3
2 11.mp3
3 22.mp3
4 已复制 1 个文件。

合并得兴起,再来一个,将密码存到图片文件里去,合并完成后,222.jpg打开仍然是一幅图片,但以文本方式打开就可以看到最后接上了passwd.txt里的内容,神奇吧.

1 D:\>copy /b 111.jpg+passwd.txt 222.jpg
2 111.jpg
3 改写 222.jpg 吗? (Yes/No/All): yes
4 passwd.txt
5 已复制 1 个文件。

ps: 不同类型的合并,需要注意文本格式(比如第一行空掉),与+号两边文件的放置顺序.  这一点这里不多讲了,自己去摸索吧.

 

4. 有用的参数/a  /b 前面已经说了,另外有/y与/-y表示覆盖时不确认与要求确认,CMD命令行中当有太多文件要求确认而你又想直接覆盖的时候,不妨用一下/y参数.

-y为CMD中默认模式,y为批处理中默认模式,这一点一定要注意,在批处理中练习这个命令时,要仔细,不然把某个盘中的文件给盖了,,你就等着哭吧.

 

5. 特殊用法:

1 copy nul a.abc 将a.abc 文件清空(文件长度改为0)
2 copy 文件名+con 向文本文件中追加命令或内容
3 copy con 文件名 创建文本文件(F6存盘退出,或者按ctrl+z键)
4 copy . d:\ 将当前目录下所有的文件都复制到d:盘下去

例子:

 1 D:\>copy con aaa.txt
2 this is a test.
3 type a txt file^Z
4 已复制 1 个文件。
5 D:\>copy nul aaa.txt
6 改写 aaa.txt 吗? (Yes/No/All): y
7 已复制 1 个文件。
8 D:\>copy aaa.txt+con
9 aaa.txt
10 con
11 test222222
12 ^Z
13 已复制 1 个文件。
14 D:\>type aaa.txt
15 test222222

 

copy比较实用也比较有意思,多多练习体会一下吧.

 OK,此篇完结.





posted @ 2011-12-10 13:27  doupip  阅读(4779)  评论(0编辑  收藏  举报