windows 批处理文件运行 xcopy 而不覆盖现有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17587347/
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 10:40:58  来源:igfitidea点击:

Batch file to run xcopy without overwriting existing files

windowsbatch-filexcopy

提问by Eae

I need my program to run:

我需要运行我的程序:

xcopy s:\* z:\ /E

When xcopy runs, it will prompt if a file needs to be overwritten, so I want the batch file to answer no in all cases to the prompt.

当xcopy运行时,它会提示是否需要覆盖文件,所以我希望批处理文件在所有情况下都对提示回答no。

How can I accomplish this?

我怎样才能做到这一点?

回答by Jeff Fischer

I almost overlooked this in the switches myself. I was aided by an Experts Exchange article.

我自己在开关中几乎忽略了这一点。我得到了一篇专家交流文章的帮助。

Here is the switch of significance:

这是重要的开关:

/D:m-d-y Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.

/D:mdy 复制在指定日期或之后更改的文件。如果未指定日期,则仅复制源时间比目标时间新的文件。

The "If no date is given" portion is of particular importance. This doesn't exactly answer the "without the overwriting existing files" question, but it does answer it if the existing file's time stamp of the source file is not newer than the destination file.

“如果没有给出日期”部分特别重要。这并不能完全回答“不覆盖现有文件”的问题,但如果源文件的现有文件的时间戳不比目标文件新,它确实回答了这个问题。

Close enough for government work.

足够接近政府工作。

回答by y-i_guy

I've been using the following flags for years. It seems to accomplish all the behaviors you describe in the question.

我多年来一直在使用以下标志。它似乎完成了您在问题中描述的所有行为。

xcopy s:\ z:\ /S /Y /D

xcopy s:\ z:\ /S /Y /D

S will copy all subfolders and files within them Y prevents any overwrite prompts D essentially copies anything newer than what is already in the destination

S 将复制其中的所有子文件夹和文件 Y 防止任何覆盖提示 D 基本上复制比目标中已有的内容更新的任何内容

回答by Martin Binder

xcopy cannot be configured to SKIP existing files, so you should copy using "robocopy".

xcopy 无法配置为跳过现有文件,因此您应该使用“robocopy”进行复制。

回答by Aditya Agarwal

This works for me:

这对我有用:

echo n|xcopy s:\* z:\ /E    

回答by hamacker

To avoid question y/n/all you can simulate "type in" using echo command as: echo n|xcopy blabla... But it′s not correct because any different question will be answer "n" too.

为避免问题 y/n/all,您可以使用 echo 命令模拟“输入”为:echo n|xcopy blabla...但这不正确,因为任何不同的问题也会回答“n”。

Better choice in cmd to replace xcopy is: robocopy source: destination: /e [/zb]

在 cmd 中替换 xcopy 的更好选择是:robocopy source: destination: /e [/zb]

In graphical mode, there is a app called "Teracopy" (use google to find it) that override windows explorer copy with several options (pause, ignore[all], overwrite[all], minimize,...) that I would like to recommend.

在图形模式下,有一个名为“Teracopy”的应用程序(使用谷歌查找它),它使用我想要的几个选项(暂停、忽略[全部]、覆盖[全部]、最小化...)覆盖 Windows 资源管理器副本推荐。

回答by Arnold Lai

Windows Command Interpreter:

Windows 命令解释器:

for /f "delims=" %i in ('dir /b /s "source"') do (echo n | xcopy "%i" "destination"  [switches] /-y)


Windows Batch:

Windows批处理:

for /f "delims=" %%i in ('dir /b /s "source"') do (echo n | xcopy "%%i" "destination"  [switches] /-y)

Please remember to make sure it prompts you before starting to copy/overwrite any files.

请记住在开始复制/覆盖任何文件之前确保它提示您。