0
複製文件在延續此: Comparing folders and content with PowerShell比較子文件夾,並從我以前的帖子使用PowerShell
...我試圖比較包含子文件夾(至少4級深)兩個目錄。第二個文件夾(folder2)包含我想要複製到第三個文件夾(folder3)的新文件和更新文件。到目前爲止,我的腳本並沒有像預期的那樣工作。第一個問題,folder2中的子文件夾需要在folder3中創建。做這個的最好方式是什麼?此外,並非所有文件(新的或更新的)都將從folder2複製到folder3。
總之,這裏是我的腳本:
$Folder1 = "D:\folder1"
$Folder2 = "D:\folder2"
$Folder3 = "D:\folder3"
function Get-Directories ($path)
{
$PathLength = $path.length
gci $path -rec | % {
Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.substring($PathLength+1)
$_
}
}
Compare-Object (Get-Directories $Folder1) (Get-Directories $Folder2) -prop RelativePath, Name, Length |
Sort RelativePath, Name, Length -desc | % {
if ($file -ne $_.RelativePath)
{ $_ }
$file = $_.RelativePath
} | Where-Object {$_.SideIndicator -eq "=>"} | ForEach-Object {Copy-Item ("$Folder2\" + $file) -Destination ("$Folder3\" + $file) -Force}