windows 复制文件而不覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4228807/
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
Copy files without overwrite
提问by Tony
I just can't seem to find a way on the command line to say "copy all the files from directory A to directory B, but if the file already exists in directory B, don't overwrite it, no matter which file is newer, and don't prompt me."
我似乎无法在命令行上找到一种方法来说“将所有文件从目录 A 复制到目录 B,但如果该文件已存在于目录 B 中,请不要覆盖它,无论哪个文件较新,不要提示我。”
I have been through copy, move, xcopy & robocopy, and the closest I can get is that you can tell robocopy "copy A to B, but don't overwrite newer files with older files," but that doesn't work for me. I looked at xxcopy, but discarded it, as I don't want to have a third-party dependency on a Visual Studio post-build event that will require other SVN users to have that tool installed in order to do the build.
我经历过复制、移动、xcopy 和 robocopy,我能得到的最接近的是你可以告诉 robocopy“将 A 复制到 B,但不要用旧文件覆盖新文件”,但这对我不起作用. 我查看了 xxcopy,但放弃了它,因为我不希望第三方依赖 Visual Studio 构建后事件,这将需要其他 SVN 用户安装该工具才能进行构建。
I want to add a command line to the post-build event in Visual Studio 2010 so that the files that are generated from the T4templates for new EFmodel objects get distributed to the project folders to which they belong, but regenerated files for existing objects don't overwrite potentially edited destination files.
我想向 Visual Studio 2010 中的生成后事件添加命令行,以便从新EF模型对象的T4模板生成的文件分发到它们所属的项目文件夹,但为现有对象重新生成文件不要覆盖可能已编辑的目标文件。
Since the T4 template regenerates, the source file is always newer, and I can't use the "newer" switch reliably, I don't think.
由于T4模板重新生成,源文件总是更新的,我认为不能可靠地使用“更新”开关。
I use partial classes for those items for which I can, but there are other things I generate that can't use partial classes (e.g. generating a default EditorTemplate or DisplayTemplate *.ascx file).
对于那些我可以使用的项目,我使用部分类,但是我生成的其他东西不能使用部分类(例如,生成默认的 EditorTemplate 或 DisplayTemplate *.ascx 文件)。
Any one have any similar problems they have solved?
有没有人解决过类似的问题?
采纳答案by Stu
For %F In ("C:\From\*.*") Do If Not Exist "C:\To\%~nxF" Copy "%F" "C:\To\%~nxF"
回答by Dr. belisarius
Robocopy, or "Robust File Copy", is a command-line directory replication command. It has been available as part of the Windows Resource Kit starting with Windows NT 4.0, and was introduced as a standard feature of Windows Vista, Windows 7 and Windows Server 2008.
的Robocopy,或者“强大的文件复制”,是一个命令行目录复制命令。从 Windows NT 4.0 开始,它已作为 Windows 资源工具包的一部分提供,并作为 Windows Vista、Windows 7 和 Windows Server 2008 的标准功能引入。
robocopy c:\Sourcepath c:\Destpath /E /XC /XN /XO
To elaborate (using Hydrargyrum, HailGallaxar and Andy Schmidt answers):
详细说明(使用 Hydrargyrum、HailGallaxar 和 Andy Schmidt 的回答):
/E
makes Robocopy recursively copy subdirectories, including empty ones./XC
excludes existing files with the same timestamp, but different file sizes. Robocopy normally overwrites those./XN
excludes existing files newer than the copy in the destination directory. Robocopy normally overwrites those./XO
excludes existing files older than the copy in the destination directory. Robocopy normally overwrites those.
/E
使 Robocopy 递归复制子目录,包括空目录。/XC
排除具有相同时间戳但文件大小不同的现有文件。Robocopy 通常会覆盖那些。/XN
排除比目标目录中的副本新的现有文件。Robocopy 通常会覆盖那些。/XO
排除比目标目录中的副本旧的现有文件。Robocopy 通常会覆盖那些。
With the Changed, Older, and Newer classes excluded, Robocopy does exactly what the original poster wants - without needing to load a scripting environment.
排除了 Changed、Older 和 Newer 类后,Robocopy 完全符合原始海报的要求——无需加载脚本环境。
References: Technet, Wikipedia
Download from: Microsoft Download Link(Link last verified on Mar 30, 2016)
参考资料:Technet、维基百科
下载来源:Microsoft 下载链接(链接最后验证日期为 2016 年 3 月 30 日)
回答by Hydrargyrum
Belisarius' solution is good.
贝利撒留的解决方案很好。
To elaborate on that slightly terse answer:
详细说明这个稍微简洁的答案:
/E
makes Robocopy recursively copy subdirectories, including empty ones./XC
excludes existing files with the same timestamp, but different file sizes. Robocopy normally overwrites those./XN
excludes existing files newer than the copy in the source directory. Robocopy normally overwrites those./XO
excludes existing files older than the copy in the source directory. Robocopy normally overwrites those.
/E
使 Robocopy 递归复制子目录,包括空目录。/XC
排除具有相同时间戳但文件大小不同的现有文件。Robocopy 通常会覆盖那些。/XN
排除比源目录中的副本新的现有文件。Robocopy 通常会覆盖那些。/XO
排除比源目录中的副本旧的现有文件。Robocopy 通常会覆盖那些。
With the Changed, Older, and Newer classes excluded, Robocopy does exactly what the original poster wants - without needing to load a scripting environment.
排除了 Changed、Older 和 Newer 类后,Robocopy 完全符合原始海报的要求——无需加载脚本环境。
回答by sachin11
You can try this:
你可以试试这个:
echo n | copy /-y <SOURCE> <DESTINATION>
-y
simply prompts before overwriting and we can pipe n to all those questions. So this would in essence just copy non-existing files. :)
-y
在覆盖之前简单地提示,我们可以将 n 传递给所有这些问题。所以这实质上只是复制不存在的文件。:)
回答by Gabe
Here it is in batch file form:
这是批处理文件形式:
@echo off
set source=%1
set dest=%2
for %%f in (%source%\*) do if not exist "%dest%\%%~nxf" copy "%%f" "%dest%\%%~nxf"
回答by Adam Says - Reinstate Monica
There is an odd way to do this with xcopy:
使用 xcopy 有一种奇怪的方法可以做到这一点:
echo nnnnnnnnnnn | xcopy /-y source target
Just include as many n's as files you're copying, and it will answer n to all of the overwrite questions.
只需包含与您要复制的文件一样多的 n,它就会回答所有覆盖问题的 n。
回答by HailGallaxar
I just want to clarify something from my own testing.
我只是想从我自己的测试中澄清一些事情。
@Hydrargyrum wrote:
@Hydrargyrum 写道:
- /XN excludes existing files newer than the copy in the source directory. Robocopy normally overwrites those.
- /XO excludes existing files older than the copy in the source directory. Robocopy normally overwrites those.
- /XN 排除比源目录中的副本新的现有文件。Robocopy 通常会覆盖那些。
- /XO 排除比源目录中的副本更旧的现有文件。Robocopy 通常会覆盖那些。
This is actually backwards. XN does "eXclude Newer" files but it excludes files that are newer than the copy in the destinationdirectory. XO does "eXclude Older", but it excludes files that are older than the copy in the destinationdirectory.
这其实是倒退。XN 执行“排除较新”文件,但它排除比目标目录中的副本新的文件。XO 执行“排除旧的”,但它排除比目标目录中的副本旧的文件。
Of course do your own testing as always.
当然,像往常一样做你自己的测试。
回答by be_good_do_good
robocopy src dst /MIR /XX
/XX : eXclude "eXtra" files and dirs (present in destination but not source). This will prevent any deletions from the destination. (this is the default)
/XX :排除“eXtra”文件和目录(存在于目标中但不存在于源中)。这将防止从目标中删除任何内容。(这是默认值)
回答by Greg
Robocopy can be downloaded here for systems where it is not installed already. (I.e. Windows Server 2003.)
对于尚未安装 Robocopy 的系统,可以在此处下载 Robocopy。(即 Windows Server 2003。)
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657(no reboot required for installation)
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657(安装无需重启)
Remember to set your path to the robocopy exe. You do this by right clicking "my computer"> properties>advanced>"Environment Variables", then find the path system variable and add this to the end: ";C:\Program Files\Windows Resource Kits\Tools" or wherever you installed it. Make sure to leave the path variable strings that are already there and just append the addtional path.
请记住将您的路径设置为 robocopy exe。您可以通过右键单击“我的电脑”>“属性”>“高级”>“环境变量”来执行此操作,然后找到路径系统变量并将其添加到末尾:“;C:\Program Files\Windows Resource Kits\Tools”或您所在的任何位置安装了它。确保保留已经存在的路径变量字符串,并仅附加附加路径。
once the path is set, you can run the command that belisarius suggests. It works great.
一旦设置了路径,您就可以运行 belisarius 建议的命令。它工作得很好。
回答by Andy Schmidt
It won't let me comment directly on the incorrect messages - but let me just warn everyone, that the definition of the /XN and /XO options are REVERSED compared to what has been posted in previous messages.
它不会让我直接对不正确的消息发表评论 - 但让我警告大家,与之前消息中发布的内容相比,/XN 和 /XO 选项的定义是颠倒的。
The Exclude Older/Newer files option is consistent with the information displayed in RoboCopy's logging: RoboCopy will iterate through the SOURCE and then report whether each file in the SOURCE is "OLDER" or "NEWER" than the file in the destination.
Exclude Older/Newer files 选项与 RoboCopy 的日志中显示的信息一致:RoboCopy 将遍历 SOURCE,然后报告 SOURCE 中的每个文件是否比目标中的文件“旧”或“新”。
Consequently, /XO will exclude OLDER SOURCE files (which is intuitive), not "older than the source" as had been claimed here.
因此, /XO 将排除 OLDER SOURCE 文件(这是直观的),而不是此处声称的“比源更旧”。
If you want to copy only new or changed source files, but avoid replacing more recent destination files, then /XO is the correct option to use.
如果您只想复制新的或更改的源文件,但避免替换最近的目标文件,则 /XO 是正确的选项。