![MS powershell 中是否有与 rsync 等效的功能?](https://linux22.com/image/1309611/MS%20powershell%20%E4%B8%AD%E6%98%AF%E5%90%A6%E6%9C%89%E4%B8%8E%20rsync%20%E7%AD%89%E6%95%88%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%9F.png)
Rsync 非常有用,我不必复制目录中的所有文件。它只更新较新的文件。
我将它与 cygwin 一起使用,但我认为存在一些不一致之处,这不是这个问题的重点。
那么,有同等的吗?
答案1
虽然不是完全等同的,也不是 Powershell 的功能,机器人复制可以完成 rsync 所用的一些功能。
答案2
这可以同步目录。调用函数“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
}
}
}
}
答案3
感谢您的脚本 rsync。我正在更新第三个选项,即清除不在源目录中的文件。
function rsync {
param ( $source,
$target
)
$WorkDir=Get-Location
Write-Output "PATH PWD: $WorkDir"
Write-Host "SOURCE: $source"
Write-Host "TARGET: $target"
$sourceFiles = Get-ChildItem -Path $source -Recurse
$targetFiles = Get-ChildItem -Path $target -Recurse
Write-Host "SOURCEFILES $sourceFiles"
Write-Host "TARGETFILES: $targetFiles"
if ($debug -eq $true) {
Write-Output "Source=$source, Target=$target"
Write-Output "sourcefiles = $sourceFiles TargetFiles = $targetFiles"
}
<#
1=1 way sync, 2=2 way sync, 3=1 way purging target files not equal with source files
#>
$syncMode = 3
#if ($sourceFiles -eq $null -or $targetFiles -eq $null) {
if ($targetFiles -eq $null)
{
$targetFiles=""
}
if ($sourceFiles -eq $null){
Write-Host "Empty Directory encountered. Skipping file Copy."
} else
{
$diff_command="Compare-Object -ReferenceObject $sourceFiles -DifferenceObject $targetFiles"
Write-Host "diff command: $diff_command"
$diff = Compare-Object -ReferenceObject $sourceFiles -DifferenceObject $targetFiles
Write-Host "---> Different Files from SOURCE to TARGET: $diff"
#$diff_target = Compare-Object -ReferenceObject $targetFiles -DifferenceObject $sourceFiles
#Write-Host "---> Different Files from TARGET to SOURCE: $diff_target"
foreach ($f in $diff) {
Write-Host "--> Indicator: $f.SideIndicator "
if ($f.SideIndicator -eq "<=") {
$fullSourceObject = $f.InputObject.FullName
$fullTargetObject = $f.InputObject.FullName.Replace($source,$target)
Write-Host "Attempt to copy the following <= SourceTarget: " $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 => TargetSource: " $fullSourceObject
Copy-Item -Path $fullSourceObject -Destination $fullTargetObject
}
if ($f.SideIndicator -eq "=>" -and $syncMode -eq 3 -and $targetFiles -ne "") {
$fullSourceObject = $f.InputObject.FullName
$fullTargetObject = $f.InputObject.FullName.Replace($target,$source)
Write-Host "Removing files from Target that are not present in Source=> TargetSource: " $fullSourceObject
Write-Host "FIles that are not in source: $fullSourceObject"
Remove-Item $fullSourceObject
}
}
}
}
##调用函数 rsync $SourcePathFull_Month_Day $DestinationPathFull_Month_Day