复制文件而不覆盖

发布于 2024-10-03 22:47:07 字数 726 浏览 1 评论 0原文

我似乎无法在命令行上找到一种方法来表示“将目录 A 中的所有文件复制到目录 B 中,但如果该文件已存在于目录 B 中,则不要覆盖它,无论哪个文件较新,并且不要提示我。”

我经历过复制、移动、xcopy 和复制。 robocopy,我能得到的最接近的是你可以告诉 robocopy“将 A 复制到 B,但不要用旧文件覆盖新文件”,但这对我不起作用。我查看了 xxcopy,但放弃了它,因为我不想对 Visual Studio 构建后事件产生第三方依赖,这将需要其他 SVN 用户安装该工具才能进行构建。

我想将命令行添加到 Visual Studio 2010 中的生成后事件,以便从 EF 模型对象的 T4 模板已分发到项目它们所属的文件夹,但现有对象的重新生成的文件不会覆盖可能已编辑的目标文件。

由于T4模板重新生成,源文件总是更新的,我不认为我不能可靠地使用“更新”开关。

我对那些可以使用的项目使用部分类,但我生成的其他内容不能使用部分类(例如生成默认的 EditorTemplate 或 DisplayTemplate *.ascx 文件)。

有人解决过类似的问题吗?

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."

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.

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 T4 templates for new EF model objects get distributed to the project folders to which they belong, but regenerated files for existing objects don't overwrite potentially edited destination files.

Since the T4 template regenerates, the source file is always newer, and I can't use the "newer" switch reliably, I don't think.

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).

Any one have any similar problems they have solved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

眼眸里的那抹悲凉 2024-10-10 22:47:07

Robocopy,或“稳健文件复制”,是一个命令行目录复制命令。从 Windows NT 4.0 开始,它作为 Windows 资源工具包的一部分提供,并作为 Windows Vista、Windows 7 和 Windows Server 2008 的标准功能引入。

   robocopy c:\Sourcepath c:\Destpath /E /XC /XN /XO

详细说明(使用 Hydrargyrum、HailGallaxar 和 Andy Schmidt 的回答) ):

  • /E 使 Robocopy 递归复制子目录,
    包括空的。
  • /XC 排除现有文件
    相同的时间戳,但不同
    文件大小。 Robocopy 通常会覆盖这些内容。
  • /XN 排除较新的现有文件
    比目的地中的副本
    目录。机器人复制通常
    覆盖那些。
  • /XO 排除
    现有文件较旧
    比目的地中的副本
    目录。机器人复制通常
    覆盖那些。

排除 Changed、Older 和 Newer 类后,Robocopy 完全按照原始发布者的要求进行操作 - 无需加载脚本环境。

参考文献:Technet维基百科
下载地址:Microsoft 下载链接 (链接最后验证时间为 2016 年 3 月 30 日)

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 c:\Sourcepath c:\Destpath /E /XC /XN /XO

To elaborate (using Hydrargyrum, HailGallaxar and Andy Schmidt answers):

  • /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.

With the Changed, Older, and Newer classes excluded, Robocopy does exactly what the original poster wants - without needing to load a scripting environment.

References: Technet, Wikipedia
Download from: Microsoft Download Link (Link last verified on Mar 30, 2016)

烧了回忆取暖 2024-10-10 22:47:07

您可以尝试以下操作:

echo n | copy /-y <SOURCE> <DESTINATION>

-y 只是在覆盖之前进行提示,我们可以通过管道 n 来解决所有这些问题。所以这本质上只是复制不存在的文件。 :)

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. :)

孤千羽 2024-10-10 22:47:07

贝利撒留的解决方案很好。

详细说明这个稍微简洁的答案:

  • /E 使 Robocopy 递归复制子目录,
    包括空的。
  • /XC 排除现有文件
    相同的时间戳,但不同
    文件大小。 Robocopy 通常会覆盖这些内容。
  • /XN 排除较新的现有文件
    比源中的副本
    目录。机器人复制通常
    覆盖那些。
  • /XO 排除
    现有文件较旧
    比源中的副本
    目录。机器人复制通常
    覆盖那些。

排除 Changed、Older 和 Newer 类后,Robocopy 完全按照原始发布者的要求进行操作 - 无需加载脚本环境。

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.

With the Changed, Older, and Newer classes excluded, Robocopy does exactly what the original poster wants - without needing to load a scripting environment.

|煩躁 2024-10-10 22:47:07
For %F In ("C:\From\*.*") Do If Not Exist "C:\To\%~nxF" Copy "%F" "C:\To\%~nxF"
For %F In ("C:\From\*.*") Do If Not Exist "C:\To\%~nxF" Copy "%F" "C:\To\%~nxF"
白云悠悠 2024-10-10 22:47:07

这是批处理文件形式:

@echo off
set source=%1
set dest=%2
for %%f in (%source%\*) do if not exist "%dest%\%%~nxf" copy "%%f" "%dest%\%%~nxf"

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"
旧瑾黎汐 2024-10-10 22:47:07

使用 xcopy 有一种奇怪的方法可以做到这一点:

echo nnnnnnnnnnn | xcopy /-y source target

只需包含与要复制的文件一样多的 n,它就会回答所有覆盖问题的 n。

There is an odd way to do this with 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.

靑春怀旧 2024-10-10 22:47:07

我只是想通过我自己的测试澄清一些事情。

@Hydrargyrum 写道:

  • /XN 排除比源目录中的副本更新的现有文件。 Robocopy 通常会覆盖这些内容。
  • /XO 排除源目录中早于副本的现有文件。 Robocopy 通常会覆盖这些内容。

这其实是本末倒置。 XN 会“排除较新的”文件,但会排除比目标目录中的副本更新的文件。 XO 会“排除较旧的”,但它会排除比目标目录中的副本更旧的文件。

当然,一如既往地进行自己的测试。

I just want to clarify something from my own testing.

@Hydrargyrum wrote:

  • /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.

This is actually backwards. XN does "eXclude Newer" files but it excludes files that are newer than the copy in the destination directory. XO does "eXclude Older", but it excludes files that are older than the copy in the destination directory.

Of course do your own testing as always.

≈。彩虹 2024-10-10 22:47:07

它不会让我直接评论不正确的消息 - 但让我警告大家,/XN 和 /XO 选项的定义与之前的消息中发布的内容相比是相反的。

排除较旧/较新的文件选项与 RoboCopy 日志记录中显示的信息一致:RoboCopy 将迭代 SOURCE,然后报告 SOURCE 中的每个文件是否比目标中的文件“旧”或“新”。

因此,/XO 将排除较旧的源文件(这是直观的),而不是像此处所声称的“比源更旧”。

如果您只想复制新的或更改的源文件,但避免替换较新的目标文件,那么 /XO 是正确的选择。

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.

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.

Consequently, /XO will exclude OLDER SOURCE files (which is intuitive), not "older than the source" as had been claimed here.

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.

余生一个溪 2024-10-10 22:47:07
robocopy src dst /MIR /XX

/XX :排除“eXtra”文件和目录(存在于目标中但不存在于源中)。这将防止从目的地进行任何删除。 (这是默认值)

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)

我不咬妳我踢妳 2024-10-10 22:47:07

对于尚未安装 Robocopy 的系统,可以在此处下载。 (即 Windows Server 2003。)

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657" microsoft.com/download/en/details.aspx?displaylang=en&id=17657
(安装无需重新启动)

请记住设置 robocopy exe 的路径。您可以通过右键单击“我的电脑”>来执行此操作属性>高级>“环境变量”,然后找到路径系统变量并将其添加到末尾:“;C:\Program Files\Windows Resource Kits\Tools”或任何安装它的位置。确保保留已存在的路径变量字符串并仅附加附加路径。

设置路径后,您可以运行 belisarius 建议的命令。效果很好。

Robocopy can be downloaded here for systems where it is not installed already. (I.e. Windows Server 2003.)

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657
(no reboot required for installation)

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.

once the path is set, you can run the command that belisarius suggests. It works great.

那片花海 2024-10-10 22:47:07

这对我有用。我用它来将文件“添加”到另一个驱动器,而不进行覆盖。

批处理文件:robocopy-missingfiles.bat

@echo off
echo Copying 
echo      "%1"
echo   to "%2"
echo.
echo Press Cntr+C to abort
Pause
echo.
@echo on
robocopy %1 %2 /Xo /XN /XC /J /SL /S /MT:8 /R:1 /W:1 /V /DCOPY:DAT /ETA /COPY:DATO /FFT /A-:SH /XD $RECYCLE.BIN "System Volume Information"

示例:

robocopy-missingfiles.bat f:\Working-folder\ E:\Backup-folder\

实施前进行测试。

This is what has worked for me. I use this to "add" files over to the other drive, with no overwrites.

Batch file: robocopy-missingfiles.bat

@echo off
echo Copying 
echo      "%1"
echo   to "%2"
echo.
echo Press Cntr+C to abort
Pause
echo.
@echo on
robocopy %1 %2 /Xo /XN /XC /J /SL /S /MT:8 /R:1 /W:1 /V /DCOPY:DAT /ETA /COPY:DATO /FFT /A-:SH /XD $RECYCLE.BIN "System Volume Information"

Example:

robocopy-missingfiles.bat f:\Working-folder\ E:\Backup-folder\

Do test before implementation.

牵你手 2024-10-10 22:47:07

我们可以使用 XCOPY 命令将文件从源文件夹复制到目标文件夹,而不覆盖目标目录中的任何现有文件。

首先使用 xcopy 选项 /L/U/Y 获取目标中已存在的文件名列表,并将其写入某个临时文件(例如 ExcludeFiles.txt)中。

然后再次使用带有 /EXCLUDE: 选项的 XCOPY 命令并提供包含需要排除的文件名列表的文件路径。
创建一个包含以下内容的批处理文件“Copy-Files.bat”并享受!如果您想要要求用户输入源目录路径和目标目录路径,请进行修改。

@echo off
set source="C:\Dir1"
set destination="C:\Dir2"

echo Copying non-existing files. NO OVERWRITE!

xcopy %source% %destination% /L/U/Y > %TEMP%\ExcludeFiles.txt
xcopy %source% %destination% /EXCLUDE:%TEMP%\ExcludeFiles.txt
pause

We can use XCOPY command to copy files from source to destination folder without overriding any existing files in target directory.

First get a list of file names which already existing in destination using xcopy option /L/U/Y and write it in some temporary file say ExcludeFiles.txt.

Then again use XCOPY command with /EXCLUDE: option and provide file path that contain list of file names that need to be excluded.
Create a batch file say "Copy-Files.bat" with below content and enjoy! Modify if you want to ask user input for source and destination directory path.

@echo off
set source="C:\Dir1"
set destination="C:\Dir2"

echo Copying non-existing files. NO OVERWRITE!

xcopy %source% %destination% /L/U/Y > %TEMP%\ExcludeFiles.txt
xcopy %source% %destination% /EXCLUDE:%TEMP%\ExcludeFiles.txt
pause
看轻我的陪伴 2024-10-10 22:47:07

如果您尝试放入文件而不覆盖现有文件,您有几种选择。

  1. 重命名这些文件,例如在文件名后添加“(1)”或“(2)”或类似的内容;这样您就可以更轻松地区分两者。

  2. 完全绕过命令行并使用Windows资源管理器。除非您没有 Windows。

我能想到的就是这些。希望这有帮助。

If you are trying to put in files without overwriting existing files, you have a few options.

  1. rename those files by, for example, putting "(1)" or "(2)" or something like it after the file name; that way you can distinguish between the two easier.

  2. completely bypass the command line and use Windows Explorer. Unless if you don't have Windows.

That is all I can think of. Hope this helps.

梅窗月明清似水 2024-10-10 22:47:07

一种简单的方法是使用 /MIR 选项来镜像这两个目录。基本上它只会将新文件复制到目的地。在下一个命令中,将源和目标替换为文件夹的路径,脚本将搜索具有任何扩展名的任何文件。

robocopy <source directory> <destination directory> *.* /MIR

A simple approach would be to use the /MIR option, to mirror the two directories. Basically it will copy only the new files to destination. In next comand replace source and destination with the paths to your folders, the script will search for any file with any extensions.

robocopy <source directory> <destination directory> *.* /MIR
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文