2

Here, I'm trying to compare 2 folders and copy the difference in the third folder recursively.

Below is my code and I'm able to do most of the part, but unable to get the files copied in the exact folder structure as if in the Folder1. This script actually copy the files directly on the folder3 without any folders. This just copies the files alone from different folder and put them in the folder3. Any help is greatly appreciated.

$Folder1path = "M:\Technical\foldercompare\folder1"
$Folder2path = "M:\Technical\foldercompare\folder2"
$Folder3path = "M:\Technical\foldercompare\folder3"
$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
}
Itchydon
  • 2,572
  • 6
  • 19
  • 33

1 Answers1

1

The script below represents one way you could approach the task (assuming your 3 directories share a common parent dir). As an aside, your initial attempt failed to handle the $nulls that occur when $Folder1Path or $Folder2Path are empty.

# Files in 1 but not in 2 should wind up in 3 with the same dir structure as occurs in 1
param(
    $Folder1path = "$PSScriptRoot\folder1",
    $Folder2path = "$PSScriptRoot\folder2",
    $Folder3path = "$PSScriptRoot\folder3"
)

$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
    }
}
derekbaker783
  • 8,109
  • 4
  • 36
  • 50
  • Kind of worked, but got a message like the "Cloud not find a part of the path '**/folder3\1\1.txt'" - Seems it looking for the file itself in the target folder which needs to be copied – pravin kumar Aug 18 '20 at 12:54
  • @pravinkumar Are you using wildcards? – derekbaker783 Aug 18 '20 at 12:56
  • It is not creating the folder in the target location. Once I created the empty folder the file is copied. I have a project, where the folder structure is complex. So it is not feasible to create folders manually. – pravin kumar Aug 18 '20 at 13:01
  • On my machine (Win10 PS 5.1), the needed folders in the destination are created via the -Force param on Copy-Item. Please see this answer for details of this approach: https://stackoverflow.com/questions/2695504/powershell-2-copy-item-which-creates-a-folder-if-doesnt-exist#answer-2696926 – derekbaker783 Aug 18 '20 at 13:04
  • And one more is my primary requirement was not just to find the missing files, but also the content changes in the files. That's why I used the -property Name, Length in mine. – pravin kumar Aug 18 '20 at 16:16