compare - Comparing Sub-Folders and Copying Files with PowerShell -


in continuation previous post here: comparing folders , content powershell

...i'm attempting compare 2 directories contain sub-folders (at least 4 levels deep). second folder (folder2) contains new , updated files want copy third folder (folder3). far script isn't quite working expected. first problem, sub-folders in folder2 need created in folder3. best way this? also, not files (new or updated) getting copied folder3 folder2.

anyway, here script:

$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} 

i made changes below seems solve problem.

function get-directories ($path) {     $pathlength = $path.length     get-childitem $path -recurse | % {         add-member -inputobject $_ -membertype noteproperty -name relativepath -value $_.fullname.substring($pathlength+1)         $_     } }  compare-object (get-directories $folder1) (get-directories $folder2) -property relativepath, name, length | sort relativepath, name, length -desc | % {     if ($file -ne $_.relativepath) { $_ } } |      where-object {$_.sideindicator -eq "=>"} |      foreach-object {         $file = $_.relativepath         echo f | xcopy "$folder2\$file" "$folder3\$file" /s     } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -