MS powershell 中是否有相当于 rsync 的功能?

kir*_*gum 21 cygwin powershell rsync

Rsync 非常有用,我不必复制目录中的所有文件。它只更新较新的文件。

我将它与 cygwin 一起使用,但我认为存在一些不一致之处,这不是这个问题的主要焦点。

那么有等价物吗?

Red*_*ick 15

尽管不是完全等效的,也不是 Powershell 功能,但robocopy可以完成 rsync 的一些用途。

另见https://serverfault.com/q/129098


小智 5

这可以同步之间的目录。调用函数“rsync”。我在使用 robocopy 时遇到了权限问题。这个就不存在这些问题了。

function rsync ($source,$target) {

  $sourceFiles = Get-ChildItem -Path $source -Recurse
  $targetFiles = Get-ChildItem -Path $target -Recurse

  if ($debug -eq $true) {
    Write-Output "Source=$source, Target=$target"
    Write-Output "sourcefiles = $sourceFiles TargetFiles = $targetFiles"
  }
  <#
  1=way sync, 2=2 way sync.
  #>
  $syncMode = 1

  if ($sourceFiles -eq $null -or $targetFiles -eq $null) {
    Write-Host "Empty Directory encountered. Skipping file Copy."
  } else
  {
    $diff = Compare-Object -ReferenceObject $sourceFiles -DifferenceObject $targetFiles

    foreach ($f in $diff) {
      if ($f.SideIndicator -eq "<=") {
        $fullSourceObject = $f.InputObject.FullName
        $fullTargetObject = $f.InputObject.FullName.Replace($source,$target)

        Write-Host "Attempt to copy the following: " $fullSourceObject
        Copy-Item -Path $fullSourceObject -Destination $fullTargetObject
      }


      if ($f.SideIndicator -eq "=>" -and $syncMode -eq 2) {
        $fullSourceObject = $f.InputObject.FullName
        $fullTargetObject = $f.InputObject.FullName.Replace($target,$source)

        Write-Host "Attempt to copy the following: " $fullSourceObject
        Copy-Item -Path $fullSourceObject -Destination $fullTargetObject
      }

    }
  }
}

Run Code Online (Sandbox Code Playgroud)