比较2个文件夹并将差异复制到另一个文件夹中-我的代码缺少一些东西



在这里,我试图比较两个文件夹,并递归地复制第三个文件夹中的差异。

下面是我的代码,我可以完成大部分工作,但无法像在Folder1中一样将文件复制到确切的文件夹结构中。这个脚本实际上是在没有任何文件夹的情况下直接复制文件夹3上的文件。这只是从不同的文件夹中单独复制文件并将其放入文件夹3。非常感谢您的帮助。

$Folder1path = "M:Technicalfoldercomparefolder1"
$Folder2path = "M:Technicalfoldercomparefolder2"
$Folder3path = "M:Technicalfoldercomparefolder3"
$Folder1 = Get-childitem -path $Folder1path -Recurse
$Folder2 = Get-childitem -path $Folder2path -Recurse
Compare-Object -ReferenceObject $folder1 -DifferenceObject $Folder2
Compare-Object $Folder1 $Folder2 -Property Name, Length  | 
Where-Object {$_.SideIndicator -eq "<="} | 
ForEach-Object {
Copy-Item "$Folder1path$($_.name)" -Destination "$Folder3path" -Force -Recurse
}

下面的脚本代表了处理任务的一种方法(假设您的3个目录共享一个公共的父目录(。顺便说一句,当$Folder1Path$Folder2Path为空时,您的初始尝试未能处理$null

# Files in 1 but not in 2 should wind up in 3 with the same dir structure as occurs in 1
param(
$Folder1path = "$PSScriptRootfolder1",
$Folder2path = "$PSScriptRootfolder2",
$Folder3path = "$PSScriptRootfolder3"
)
$ErrorActionPreference = "Stop";
Set-StrictMode -Version 'Latest'
Get-ChildItem -Path $Folder1Path -Recurse | Where-Object {
[string] $toDiff = $_.FullName.Replace($Folder1path, $Folder2path)
# Determine what's in 2, but not 1
[bool] $isDiff = (Test-Path -Path $toDiff) -eq $false

if ($isDiff) {
# Create destination path that contains folder structure
$dest = $_.FullName.Replace($Folder1path, $Folder3path)
Copy-Item -Path $_.FullName -Destination $dest -Verbose -Force
}
}

相关内容

  • 没有找到相关文章

最新更新