编程小贴士

给你的编程提供小点子


DOS的XCOPY命令的使用介绍

1、XCOPY命令

可以在cmd中录入:XCOPY /? 即可查看帮助

帮助:

XCOPY

Microsoft Windows [版本 6.2.9200]
(c) 2012 Microsoft Corporation。保留所有权利。

C:\Users\Administrator>xcopy /?
复制文件和目录树。

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
[/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
[/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B] [/J]
[/EXCLUDE:file1[+file2][+file3]…]

source       指定要复制的文件。
destination  指定新文件的位置和/或名称。
/A           仅复制有存档属性集的文件,
但不更改属性。
/M           仅复制有存档属性集的文件,
并关闭存档属性。
/D:m-d-y     复制在指定日期或指定日期以后更改的文件。
如果没有提供日期,只复制那些
源时间比目标时间新的文件。
/EXCLUDE:file1[+file2][+file3]…
指定含有字符串的文件列表。每个字符串
在文件中应位于单独的一行。如果任何
字符串与复制文件的绝对路径的任何部分相符,
则排除复制该文件。例如,
指定如 \obj\ 或 .obj 的字符串会分别
排除目录 obj 下面的所有文件或带有
.obj 扩展名的所有文件。
/P           创建每个目标文件之前提示你。
/S           复制目录和子目录,不包括空目录。
/E           复制目录和子目录,包括空目录。
与 /S /E 相同。可以用来修改 /T。
/V           验证每个新文件的大小。
/W           提示你在复制前按键。
/C           即使有错误,也继续复制。
/I           如果目标不存在,且要复制多个文件,
则假定目标必须是目录。
/Q           复制时不显示文件名。
/F           复制时显示完整的源文件名和目标文件名。
/L           显示要复制的文件。
/G           允许将加密文件复制到
不支持加密的目标。
/H           也复制隐藏文件和系统文件。
/R           覆盖只读文件。
/T           创建目录结构,但不复制文件。不
包括空目录或子目录。/T /E 包括
空目录和子目录。
/U           只复制已经存在于目标中的文件。
/K           复制属性。一般的 Xcopy 会重置只读属性。
/N           用生成的短名称复制。
/O           复制文件所有权和 ACL 信息。
/X           复制文件审核设置(隐含 /O)。
/Y           取消提示以确认要覆盖
现有目标文件。
/-Y          要提示以确认要覆盖
现有目标文件。
/Z           在可重新启动模式下复制网络文件。
/B           复制符号链接本身与链接目标。
/J           复制时不使用缓冲的 I/O。推荐复制大文件时使用。

开关 /Y 可以预先在 COPYCMD 环境变量中设置。
这可能被命令行上的 /-Y 覆盖。

C:\Users\Administrator>

认真读懂这些参数,即可写Copy命令了。

简单的:

xcopy source target

下面介绍几种常见用法:

1)拷贝文件

将C盘的info.txt文件拷贝到D盘根目录。

xcopy C:\info.txt D:\

将命令保存到bat文件,然后在C盘创建info.txt,执行bat文件,即可在D盘看到。

2)指定参数,强制覆盖

如果重复执行,因为文件已经存在,则会给出提示

tips1000_2014-08-26_00-42-40

我们加入强制执行参数:

xcopy C:\info.txt D:\  /Y

我们从上文的帮助中砍下其命令说明:

/Y           取消提示以确认要覆盖
               现有目标文件。
/-Y          要提示以确认要覆盖
               现有目标文件。

然后,就可以重复执行,每次都会覆盖。

3)拷贝文件,只拷贝更新文件

每次都强制copy,如果源文件比目标目录的文件,日期新,才copy怎么办呢?

增加配置参数:D

xcopy C:\info.txt D:\  /Y/D

对应才参数说明:

/D:m-d-y     复制在指定日期或指定日期以后更改的文件。
               如果没有提供日期,只复制那些
               源时间比目标时间新的文件。

4)拷贝目录

其实与拷贝文件类似。

xcopy C:\test D:\test  /D/Y

如果D:\test文件夹不存在,则会自动创建。

在C盘的test文件中创建文件和文件夹,即可进行Copy。

第一次执行会出现提示:是文件还是目录。

tips1000_2014-08-26_00-42-401

如何指定其为目录,无需客户判定呢?

解决办法:默认是目录:/I

xcopy C:\test D:\test2  /I/D/Y

其使用说明:

 /I           如果目标不存在,且要复制多个文件,
               则假定目标必须是目录。

5)拷贝目录中包含子目录

根据上面的脚本,只能复制当前目录下的文件及目录

我们可以使用/S 或者/E

xcopy C:\test D:\test2  /E/I/D/Y

这样,其C:\test下的所有文件,包括子目录与空目录,以及子目录的子目录文件,也都成功拷贝到了目标目录。

/S与/E具体解释:

 /S           复制目录和子目录,不包括空目录。
  /E           复制目录和子目录,包括空目录。
               与 /S /E 相同。可以用来修改 /T。

6)如果Copy只读文件

当文件Copy到目标目录,如果其是或者设置为只读文件,则无法覆盖。

tips1000_2014-08-26_00-42-402

这时我们用到了参数/R

xcopy C:\test D:\test  /E/R/I/D/Y

具体解释:

/R           覆盖只读文件。

其他的命令参数,我们这里不再介绍,根据自己的需要,认真研究帮助文档。

2、echo命令,增加描述

通过XCOPY命令,可以很轻松的实现copy功能。为了让界面更友好,我们希望将执行的情况展现给用户。

这时候就需要echo命令。

在cmd中输入:echo /?查看帮助

C:\Users\Administrator>echo /?
显示信息,或启用或关闭命令回显。

  ECHO [ON | OFF]
  ECHO [message]

要显示当前回显设置,键入不带参数的 ECHO

 

几种常见功能介绍:

1)输出提示信息

格式:ECHO信息内容

echo HelloWorld

执行结果:

tips1000_2014-08-26_00-42-402

2)作为控制批处理命令在执行时是否显示命令行自身的开关

格式:ECHO [ON|OFF]
如果想关闭“ECHO OFF”命令行自身的显示,则需要在该命令行前加上“@”。

在上图中,我们可以看到前面总是有路径信息,很烦,能否只展现dos命令执行信息呢?这就要用到ECHO OFF

@echo OFF
echo HelloWorld
pause

执行结果:

tips1000_2014-08-26_00-42-403

看到了,cmd窗口只剩下dos命令了。简洁漂亮。

3)echo还有其他功能,如改变背景色、输出文件等。这里不再介绍。

3、SET命令,设置变量

用户录入还需要给出具体的提示。

但是,我们希望将文件拷贝到用户指定的目录中去。

需要提供一个交互页面,指导用户进行操作。

这就需要dos的set命令。用来设置变量。

SET [variable=[string]]

variable   指定环境变量名。
string 指定要指派给变量的一系列字符串。 要显示当前环境变量,键入不带参数的 SET。 SET 命令不允许变量名含有等号。

但获取变量时,用%variable%获取

在cmd中可以输入:SET /? 查看详细帮助。

1)设置变量

例子:

@echo OFF
set name=Tom
echo %name%

 

2)给出输入提示,则需要/p参数

SET /P variable=[promptString]

看一个helloworld的例子

echo
set /p name=请输入您的名字
echo Hello %name%,welcome to dos world.
pause

3)用set命令进行简单计算

语法:

SET /A expression

 

/A 命令行开关指定等号右边的字符串为被评估的数字表达式。该表达式评估器很简单并以递减的优先权顺序支持下列操作。

例子:1+1=2

@echo OFF
set /p expresion=请输入数学表达式
set /a result = %expresion%
echo %result%
pause

关于set可以进行计算的功能,还有很多详细的内容,这里不再多说,可以查询相关的资料。

4)set用于字符串处理

1)字符串替换

语法:

%var:str1=str2%

含义:将字符串变量%var%中的str1替换为str2

 

例子:

@echo OFF
set var=坚持就是胜利
echo 替换前的值为:"%var%"
set var=%var:是=能%
echo 替换后的值为:%var%
pause

执行结果:

tips1000_2014-08-26_00-42-41

2)字符串截取

语法:

%var:~[m[,n]]%

方括号表示可选,%为变量标识符,var为变量名,不可少,冒号用于分隔变量名和说明部分,符号~可以简单理解为“偏移”即可,m为偏移量(缺省为0),n为截取长度(缺省为全部) %PATH:~10,5%   

例子:

@echo OFF
set var=坚持就是胜利
echo 截取前:%var%%
set var="%var:~0,2%"
echo 截取后:%var%
pause

结果:

tips1000_2014-08-26_00-42-42

截取长度可以为负数:

语法:

%var:~0,-2%

含义:把字符串倒数第三位开始的所有值付给变量。

负数,代表逆向。

@echo OFF
set var=坚持就是胜利
echo 截取前:%var%%
set var="%var:~0,-2%"
echo 截取后:%var%
pause

结果:

tips1000_2014-08-26_00-42-421

 

其实位置可以省略

语法:

%var:~-2%

含义:从变量var字符串,从末尾开始,截取2个字符。

@echo OFF
set var=坚持就是胜利
echo 截取前:%var%%
set var="%var:~-2%"
echo 截取后:%var%
pause

结果:

tips1000_2014-08-26_00-42-423

 

4、IF命令,判断

设置了路径,我们需要判断是否存在,通过IF语句

在cmd中输入:IF /?  查看if语句的使用帮助。

IF帮助

这里举两个简单例子

1)判断字符串

@echo OFF
SET /P var=是否继续执行,请输入Y或N
ECHO 输入的值为:"%var%"
IF "%var%"=="Y" (
    echo 继续执行...
)ELSE (
    echo 退出中...
)
    
pause

 2)判断文件是否存在

@echo OFF
set /P var=请输入文件名,包括文件路径,如:C:\Info.txt
echo 输入的值为:"%var%"
if exist %var%. (echo 文件存在. ) else echo 文件不存在.    
pause

备注:if与else在同一行中

如果是分行处理,一定要保证括号附近有空格,否则会报语法错误,如下:

@echo OFF
set /P var=请输入文件名,包括文件路径,如:C:\Info.txt  
echo 输入的值为:"%var%"
if exist %var% (
echo 文件存在
) else (
echo 文件不存在
)    
pause

执行结果:

tips1000_2014-08-26_00-42-422

5、goto语句,用于语句跳转

如果对不同的情况,需要执行不同的既定操作,若还是按照常规的执行流程的话,是无法完成任务的,这个时候,就需要引入流程跳转的概念,动用流程跳转语句 goto 了。流程跳转的含义是:改变默认的执行顺序,强制跳转到指定的位置执行特定的程序块。

先来看个例子:假设需要判断用户输入的是A还是B,代码可以写成这样:

@echo off 
set /p input=请输入字母A或B: 
if "%input%"=="A" goto A 
if "%input%"=="B" goto B 
pause 
exit 

:A 
echo 您输入的字母是A 
pause 
exit 

:B 
echo 您输入的字母是B 
pause 
exit 

6、pause命令,暂停批处理程序

暂停批处理程序,并显示以下消息:
请按任意键继续. . .

7、rem命令,注释

为了bat脚本更好看,需添加注释,就需要rem命令

语法:

REM [COMMENT]

在文件开头写说明性文字如:

REM 拷贝文件
REM CREATEBY YANK
REM 2014-08-25

如果注释多行,可以采取goto语句

goto Main
拷贝文件
CREATEBY YANK
2014-08-25
:Main

 最后的拷贝脚本如下:

goto MAIN
拷贝文件
CREATEBY YANK
2014-08-25
:MAIN

@echo off
echo "Copy start"

echo 请输入需要拷贝的目标路径
echo 请输入target的路径,如:D:\target
:SETTARGET
set /p target=请输入target的路径:
if "%target%"=="" (
    echo 未输入target的路径
    echo 补丁文件安装失败
    echo 退出...
    pause
    goto QUIT
)
if not exist "%target%" (
    echo 输入的target地址不存在,请检查后重新输入
    goto SETTARGET
) else (
    xcopy C:\test %target%  /E/R/I/D/Y
)

:QUIT

 

其他

1、bat执行,汉字出现乱码

tips1000_2014-08-26_00-42-43

找了很多,最终,将bat文件用记事本打开,另存为ANSI格式即可。之前是UTF-8。

89 Responses to “ DOS的XCOPY命令的使用介绍 ”

  1. best pron说道:

    WuMhet Very nice post. I just stumbled upon your blog and wanted to say that I ave truly enjoyed browsing your blog posts. In any case I all be subscribing to your feed and I hope you write again soon!

  2. chocopie说道:

    ar92Mb wow, awesome blog article.Much thanks again. Much obliged.

  3. Woman of Alien Great do the job you have completed, this page is actually interesting with wonderful facts. Time is God as way of keeping every little thing from occurring at once.

  4. animal coloring说道:

    You have noted very interesting details ! ps decent web site. He that will not sail till all dangers are over must never put to sea. by Thomas Fuller.

  5. Mca scam说道:

    we came across a cool web-site that you just may possibly delight in. Take a appear in case you want

  6. Your content is excellent but with pics and videos, this blog could undeniably be one of the best in its field.

  7. Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is excellent, let alone the content!

  8. This very blog is definitely awesome and besides factual. I have chosen a lot of helpful advices out of this blog. I ad love to go back every once in a while. Thanks!

  9. Oluwadamilare说道:

    Very clear site, thankyou for this post.

  10. Wohh exactly what I was looking for, appreciate it for posting.

  11. website说道:

    This site definitely has all the information I wanted about this

  12. w88 world说道:

    Thanks for sharing this first-class piece. Very interesting ideas! (as always, btw)

  13. It as actually a cool and helpful piece of info. I am glad that you shared this useful info with us. Please keep us up to date like this. Thanks for sharing.

  14. tui deo cheo nu说道:

    It as not that I want to replicate your web site, but I really like the style. Could you let me know which style are you using? Or was it especially designed?

  15. Woking taxi说道:

    Say, you got a nice blog post.Really thank you! Want more.

  16. cua tu dong说道:

    There as definately a lot to learn about this topic. I really like all the points you made.

  17. Wow, great blog post.Thanks Again. Really Cool.

  18. find more here说道:

    together considerably far more and a lot more typical and it may very well be primarily an extension of on the internet courting

  19. Citizenship说道:

    pretty handy material, overall I think this is well worth a bookmark, thanks

  20. Nwokolo说道:

    Very good post.Much thanks again. Awesome.

  21. Spot on with this write-up, I truly feel this web site needs a lot more attention. I all probably be back again to read more, thanks for the info!

  22. It is best to participate in a contest for one of the best blogs on the web. I will recommend this website!

  23. Thanks again for the blog.Thanks Again. Will read on

  24. Major thanks for the article post.Really thank you! Really Great.

  25. simple tweeks would really make my blog stand out. Please let me know

  26. budget dumpster说道:

    There is apparently a lot to realize about this. I think you made some nice points in features also.

  27. This text is worth everyone as attention. Where can I find out more?|

  28. to read more说道:

    Please let me know if you are looking for a article author for

  29. securities说道:

    Say, you got a nice post.Really looking forward to read more. Will read on

  30. for more info说道:

    some times its a pain in the ass to read what blog owners wrote but this internet site is very user pleasant!.

  31. mens fashion说道:

    pretty helpful stuff, overall I think this is really worth a bookmark, thanks

  32. Some genuinely interesting details you have written.Helped me a lot, just what I was searching for .

  33. Im thankful for the blog post.Thanks Again. Fantastic.

  34. fashion说道:

    It as hard to find knowledgeable people on this topic, but you sound like you know what you are talking about! Thanks

  35. Thank you for your post.Much thanks again. Will read on

  36. Hosting说道:

    Very interesting information!Perfect just what I was searching for! Being rich is having money being wealthy is having time. by Margaret Bonnano.

  37. Sisimiut说道:

    This web site truly has all the info I needed about this subject and didn at know who to ask.

  38. local说道:

    informatii interesante si utile postate pe blogul dumneavoastra. dar ca si o paranteza , ce parere aveti de inchirierea apartamente vacanta ?.

  39. white shoes说道:

    wow, awesome blog article.Thanks Again. Really Cool.

  40. worth说道:

    Just because they call it advanced doesn at mean it is.

  41. kouponkabla.com说道:

    is written by him as nobody else know such detailed about my problem.

  42. It is super blog, I would like to be like you

  43. School admission说道:

    we came across a cool website which you could appreciate. Take a look for those who want

  44. It?s really a great and helpful piece of info. I am glad that you simply shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

  45. the most beneficial in its field. Awesome blog!

  46. agen sbobet说道:

    Incredible story there. What happened after? Take care!

  47. Inspiring quest there. What happened after? Good luck!

  48. Usually I do not read article on blogs, however I would like to say that this write-up very pressured me to try and do so! Your writing taste has been amazed me. Thanks, quite great post.

  49. tm objection说道:

    Thanks-a-mundo for the blog article.Really thank you! Want more.

  50. Yeah bookmaking this wasn at a high risk decision outstanding post!.

  51. You ave made some good points there. I looked on the net to find out more about the issue and found most individuals will go along with your views on this site.

  52. Whispering Misty So sorry you are going to pass up the workshop!

  53. Way cool! Some extremely valid points! I appreciate you penning this post and the rest of the site is also very good.

  54. free browsing说道:

    This site was how do I say it? Relevant!! Finally I ave found something that helped me. Thanks a lot!

  55. tarot amor si o no horoscopo de hoy tarot amigo

  56. wow, awesome blog article.Really looking forward to read more. Awesome.

  57. pool builders说道:

    Wow! Thank you! I continuously needed to write on my site something like that. Can I implement a fragment of your post to my site?

  58. accident lawyer说道:

    Major thanks for the blog.Really looking forward to read more. Great.

  59. Wow, great post.Really looking forward to read more. Really Cool.

  60. Some really select content on this internet site , saved to bookmarks.

  61. Really appreciate you sharing this blog post.Much thanks again. Really Great.

  62. You made some clear points there. I did a search on the subject matter and found most people will approve with your site.

  63. wp commission说道:

    I was seeking this particular information for a long time.

  64. more info说道:

    is excellent but with pics and videos, this website could undeniably be one of

  65. I value the article post.Really thank you! Awesome.

  66. Really informative article post.Much thanks again. Really Great.

  67. This is really interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your fantastic post. Also, I have shared your website in my social networks!

  68. You have brought up a very wonderful details , thanks for the post.

  69. Okey说道:

    my car charger is well made and very tough. i use it all the time a* a

  70. check说道:

    There is apparently a lot to identify about this. I assume you made various nice points in features also.

  71. miller electric说道:

    Whoa! This blog Whoa! This blog looks just like my old one! It as on a totally different subject but it has pretty much the same layout and design. Outstanding choice of colors!

  72. There is visibly a bunch to identify about this. I feel you made certain nice points in features also.

  73. Please permit me understand in order that I may just subscribe. Thanks.

  74. check out说道:

    There is perceptibly a bundle to identify about this. I feel you made various good points in features also.

  75. to learn more说道:

    This is really interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your fantastic post. Also, I ave shared your website in my social networks!

  76. I think other web site proprietors should take this web site as an model, very clean and wonderful user friendly style and design, let alone the content. You are an expert in this topic!

  77. Keo dan gach说道:

    Ultimately, an issue that I am passionate about. I have looked for data of this caliber for the very last various hrs. Your website is tremendously appreciated.

  78. caratulas CD说道:

    physical exam before starting one. Many undersized Robert Griffin Iii Jersey Price

  79. Danny Nimmo说道:

    My spouse and I stumbled over here from a different website and thought I may as well check things out. I like what I see so now i’m following you. Look forward to going over your web page repeatedly.

  80. casino说道:

    Thanks-a-mundo for the post.Thanks Again. Fantastic.

  81. It as hard to come by educated people in this particular subject, but you seem like you know what you are talking about! Thanks

  82. gohere说道:

    Wow, great blog.Really looking forward to read more. Want more.

  83. tovary optom说道:

    There is obviously a bundle to know about this. I feel you made various good points in features also.

  84. Outstanding place of duty, you have critical absent a quantity of outstanding points, I also imagine this is a fantastically admirable website.

  85. we prefer to honor a lot of other online sites around the net, even though they aren

  86. Magnificent website. A lot of helpful information here. I am sending it to several buddies ans also sharing in delicious. And obviously, thanks in your sweat!

  87. Very good post. I definitely appreciate this site. Stick with it!

  88. check here说道:

    Your stream posts constantly contain a lot of especially positive to date information. Everyplace achieve you come positive with this? Emphatically stating you are same creative. Recognition again

  89. This is a topic that as near to my heart Cheers! Exactly where are your contact details though?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>