0

Here what I'm trying to do and this is my second post on the same issue, I'm trying to compare 2 folders and copy the difference in the 3rd folder with the same folder structure recursively.

Here is the Powershell script which I receive from an another post (how to copy updated files to new folder and create directory as source if not exist?). This code does everything I need but I have one problem, with the -passthru arguement, it take the file which ever is new in the "folder 2" as well. What I need is what ever files are different in Folder1 from Folder2 alone to be copied over to Folder3. I tried and could not make it work. Any help will be great.

       $newdir = "M:\Technical\foldercompare\folder1"
    $olddir = "M:\Technical\foldercompare\folder2"
    $diffdir = "M:\Technical\foldercompare\folder3"

#Get the list of files in oldFolder
$oldfiles = Get-ChildItem -Recurse -path $olddir | Where-Object {-not ($_.PSIsContainer)}

#get the list of files in new folder
$newfiles = Get-ChildItem -Recurse -path $newdir | Where-Object {-not ($_.PSIsContainer)}

Compare-Object $oldfiles $newfiles -Property LastWriteTime,Length -Passthru | sort LastWriteTime | foreach {
     $fl = (($_.Fullname).ToString().Replace("$olddir","$diffdir")).Replace("$newdir","$diffdir")
     $dir = Split-Path $fl
     If (!(Test-Path $dir)){
        mkdir $dir
     }
     copy-item $_.Fullname $fl
}
Adis1102
  • 192
  • 1
  • 11
  • 1
    What do you mean, SO doesn't allow posting the code? You shouldn't be posting _images_ about code but the _actual_ code. Do you get an error message from SO? What does it say? – vonPryz Aug 21 '20 at 05:48
  • Just select and copy the code from your IDE, than paste it into the StackOverflow textbox and click `{}` to format it as code. See also: https://stackoverflow.com/help/formatting – iRon Aug 21 '20 at 06:07
  • Tried those, and with no option.. I posted the pic – pravin kumar Aug 23 '20 at 13:09
  • Added the code and it is not accepting in a write format as in my PS IDE. But here is the actual code. – pravin kumar Aug 23 '20 at 13:14

0 Answers0