Compare files between 2 folders?

rated by 0 users
This post has 5 Replies | 3 Followers

Top 200 Contributor
Posts 15
kelster101757 Posted: 01-09-2013 10:24 AM

A company sales employee has left our company to go to a competitor. He has files in on his work laptop local hard drive.  

We are trying to establish if there are any files that exist on his computer but not on the shared network folder. I need to produce a list of any files (along with their paths) that are present on his laptop but not on the share network location. The file structure between the laptops local hard drive and the shared network location are different. Its only file names i'm interested in comparing as the location between the 2 sets of folders have different structures.  The definition of a match is if file name and size are the same.  I've created the code below but it doesnt give me the path? Can anyone help me solve this problem? 

 

$folderAcontent = "C:\temp\test1" 
$folderBcontent = "C:\temp\test2"

$FolderAContents = Get-ChildItem $folderAcontent -Recurse | where-object {!$_.PSIsContainer}
$FolderBContents = Get-ChildItem $folderBcontent -Recurse | where-object {!$_.PSIsContainer}

$FolderList = Compare-Object -ReferenceObject ($FolderAContents ) -DifferenceObject ($FolderBContents) -Property name
$FolderList 

 

Top 500 Contributor
Posts 14

use the -PassThru parameter

$FolderList = Compare-Object -ReferenceObject ($FolderAContents ) -DifferenceObject ($FolderBContents) -Property name -PassThru

foreach ($obj in $FolderList)

{

$obj.FullName # this property is the full path

}

Top 10 Contributor
Posts 1,762
Microsoft MVP
Top Contributor

Try this one, to get only the files in one of the folders use appropriate where-object afterwards. 

 

$folderReference = "C:\temp\" 
$folderDifference = "C:\Temp\test1"

$FolderReferenceContents = Get-ChildItem $folderReference -Recurse | where-object {-not $_.PSIsContainer}
$FolderDifferenceContents = Get-ChildItem $folderDifference -Recurse | where-object {-not $_.PSIsContainer}

Compare-Object -ReferenceObject $FolderReferenceContents -DifferenceObject $FolderDifferenceContents -Property ('Name', 'Length')
Top 200 Contributor
Posts 15

I've used the "-passthru" parameter and the code executed without error and I saw the difference in files between 2 folders in my test environment.  However, when I ran the script for the live folders there were objects for file in FolderA "passthrough" that were also in folderB.  Any ideas why this would be? 

 

$folderAcontent = "C:\temp\test1" 

$folderBcontent = "C:\temp\test2"

 

$FolderAContents = Get-ChildItem $folderAcontent -Recurse | where-object {!$_.PSIsContainer}

$FolderBContents = Get-ChildItem $folderBcontent -Recurse | where-object {!$_.PSIsContainer}

 

$FolderListT = Compare-Object -ReferenceObject ($FolderAContents ) -DifferenceObject ($FolderBContents) -Property name -PassThru

 

foreach ($obj in $FolderListT)

 

{

 

$obj.FullName # this property is the full path

 

}

 

Top 200 Contributor
Posts 15

I've added the the length.  I also need the "-passthrough" so I get the ".fullpath" of the input object.  Thanks for your help :-) 

Top 10 Contributor
Posts 1,762
Microsoft MVP
Top Contributor

Not sure if you made it work. this should be what you need:

$folderReference = "serverpath" 
$folderDifference = "laptop path"

$FolderReferenceContents = Get-ChildItem $folderReference -Recurse | 
    where-object {-not $_.PSIsContainer}
$FolderDifferenceContents = Get-ChildItem $folderDifference -Recurse | 
    where-object {-not $_.PSIsContainer}

#get only files that are on laptop not on server
Compare-Object -ReferenceObject $FolderReferenceContents `
-DifferenceObject $FolderDifferenceContents -Property ('Name', 'Length') -PassThru |
    where-object { $_.SideIndicator -eq '=>'} | 
        select FullName
Page 1 of 1 (6 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.