Robocopy 是否默认跳过复制现有文件?

Robocopy 是否默认跳过复制现有文件?

我有(某处)Robocopy默认情况下跳过复制现有文件。

但是...我找不到任何命令行开关为了那个原因。

答案1

默认情况下,Robocopy 会跳过复制现有文件如果文件的特定元数据匹配然后那些文件将被跳过来自“文件”复制操作(/COPY:DAT)。

Robocopy 默认值

  • 跳过文件复制,如果最后写入时间,文件名,文件大小匹配
  • 复制文件(如果最后写入时间、文件名、或者文件大小不匹配

正如所指出的@mklement0, 这默示违约/COPY:DAT如果时间戳 [LastWriteTime] 和文件大小相同,则不会复制具有不同数据的文件,因此这是默认跳过。

因此,如果由于某种原因,您要同步两个文件,它们的文件大小、文件名和上次修改属性相匹配,即使数据不同,它也不会复制源文件。

Robocopy 默认选项: /COPY:DAT /R:1000000 /W:30

通过日志文件或命令窗口运行确认robocopy c:\source c:\dest *


Robocopy或 Robocopy /?

 /COPY:copyflag[s] : What to COPY (default is /COPY:DAT)
                      (copyflags : D=Data, A=Attributes, T=Timestamps
                       S=Security=NTFS ACLs, O=Owner info, U=aUditing info).

答案2

robocopy SOURCE DESTINATION FILE(S) /IS

哪里IS代表包括年代同名文件。使用此开关会覆盖现有文件。见下文:

::
:: File Selection Options :
::
                 /A :: copy only files with the Archive attribute set.
                 /M :: copy only files with the Archive attribute and reset it.
    /IA:[RASHCNETO] :: Include only files with any of the given Attributes set.
    /XA:[RASHCNETO] :: eXclude files with any of the given Attributes set.

 /XF file [file]... :: eXclude Files matching given names/paths/wildcards.
 /XD dirs [dirs]... :: eXclude Directories matching given names/paths.

                /XC :: eXclude Changed files.
                /XN :: eXclude Newer files.
                /XO :: eXclude Older files.
                /XX :: eXclude eXtra files and directories.
                /XL :: eXclude Lonely files and directories.
                /IS :: Include Same files.
                /IT :: Include Tweaked files.

答案3

  • 不幸的是,官方文档没有描述背后的逻辑哪些文件跳过默认情况下

  • 非官方ss64.com 文档然而,提供了关键的提示(强调):

    • 默认情况下,Robocopy 仅会在源文件和目标文件具有以下特征时才复制文件:不同的时间戳或不同的文件大小

笔记:时间戳指的是上一次更改时间戳(仅有的)。

换句话说:Robocopy 认为两个文件是相同的基于仅有的它们的最后修改时间戳和文件大小是否相同,因此跳过在这种情况下进行复制。

据我所知:

  • 这种行为不是受修改影响复制文件/目录的哪些部分(参数/copy/ /dcopy

    • 这些参数仅适用于如果RoboCopy 从根本上根据相同性/包含逻辑判定给定的文件/目录需要复制。
  • 如果你想延长默认相同性检测(相同的最后修改时间戳和相同的文件大小)到遵循属性, 使用/it包括调整选项:

    • 文件属性(表示为A参数/copy
    • ACL(表示S/copy参数)
    • 文件所有权(表示为O参数/copy
    • 审计信息(表示为U参数/copy
  • Robocopy 似乎提供根据文件检测其相同性的选项内容(通常通过加密哈希函数
    因此,在罕见事件可能会有具有相同最后修改时间戳和相同文件大小的文件,但内容,你唯一的选择是使用/is包括相同选项:

    • 这会导致被认为相同的文件被复制无条件地- 因此可能是不必要的。

    • 警告:奇怪的是,文件在最后修改的文件戳和文件大小方面是相同的但在/it上述相关属性方面有所不同不是默认情况下包含在/isalone中;因此,以应对所有可能发生的情况,使用/is/it 合并


下列纠缠测试演示了行为;将代码保存为Tests.ps1并以如下方式调用Invoke-Pester ./Tests.ps1

Describe RoboCopySkippedFilesTests {
  BeforeAll {
    Push-Location TestDrive:
  }
  AfterAll {
    Pop-Location
  }
  BeforeEach {
    # Set up a source and a destination folder and make their
    # Content the same.
    if (Test-Path dest) { Remove-Item -Force -Recurse dest }
    $null = New-Item -Type Directory -Force src
    'file1' > src\file1
    'file2' > src\file2
    Copy-Item -Recurse src dest
  }

  It "Does not copy anything with identical folders." {
    robocopy.exe src dest
    $LASTEXITCODE | Should Be 0
  }
  It "Does recognize a changed last-modified filestamp" {
    (gi src\file1).LastWriteTime = [datetime]::now
    robocopy.exe src dest # | Out-Host
    $LASTEXITCODE | Should Be 1
  }
  It "Does recognize a change in file size" {
    '!' | Add-Content dest\file1
    robocopy.exe src dest # | Out-Host
    $LASTEXITCODE | Should Be 1
  }
  It "Does not recognize a change in file content, with size and last-modified date identical" {
    'file!' > dest\file1
    (Get-Item dest\file1).LastWriteTime = (Get-Item src\file1).LastWriteTime
    robocopy.exe src dest # | Out-Host
    $LASTEXITCODE | Should Be 0
  }
  It "Does not recognize a change in file attributes, with size and last-modified date identical" {
    (Get-Item dest\file1).Attributes = 'System'
    robocopy.exe src dest # | Out-Host
    $LASTEXITCODE | Should Be 0
  }
  It "Does recognize an attribute-modified-only file as tweaked (/IT)" {
    (Get-Item dest\file1).Attributes = 'System'
    robocopy.exe src dest /IT # | Out-Host
    $LASTEXITCODE | Should Be 1
  }
  It "Does not include an attribute-modified-only file with /IS" {
    Remove-Item src\file2, dest\file2
    (Get-Item dest\file1).Attributes = 'System'
    robocopy.exe src dest /IS # | Out-Host
    $LASTEXITCODE | Should Be 0
  }
}

您应该看到类似下面的内容,表明所有测试均已通过(截至Robocopy.exe文件版本10.0.16299.15 (WinBuild.160101.0800)):

Describing RoboCopySkippedFilesTests
 [+] Does not copy anything with identical folders. 231ms
 [+] Does recognize a changed last-modified filestamp 112ms
 [+] Does recognize a change in file size 68ms
 [+] Does not recognize a change in file content, with size and last-modified date identical 69ms
 [+] Does not recognize a change in file attributes, with size and last-modified date identical 83ms
 [+] Does recognize an attribute-modified-only file as tweaked (/IT) 65ms
 [+] Does not include an attribute-modified-only file with /IS 70ms
Tests completed in 589ms
Passed: 7 Failed: 0 Skipped: 0 Pending: 0 Inconclusive: 0

相关内容