• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Powershell that compairs folders

I'm putting together a power shell that compares two folders, but I'm having a bit of trouble getting it do to what I want.

What I"m looking to do is compare folder A with folder B and list all the files that are different.  In the list would be the folder location, file, and date/time stamp.  So it would look like :

Folder A                 Timestamp                        Folder B                  Timestamp
c:\temp\file.txt      9/15/2012/ 7:11 AM         x:\some\file.txt      9/15/2012/ 8:04 AM

thoughts?
0
Mach03
Asked:
Mach03
  • 2
1 Solution
 
Dale HarrisCommented:
I believe the meat and potatoes can be found in this article I wrote a while back:
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/A_4400-Powershell-Update-Scripts-Across-Multiple-Computers.html

Good luck!

DH
0
 
QlemoC++ DeveloperCommented:
You should think it is an easy task with PowerShell - but it isn't.
Using associative arrays (hash tables), we can say
$folderA = 'C:\temp\ee\tst\1'
$folderB = 'C:\temp\ee\tst\2'
$files = @{}
Compare-Object (gci $folderA) (gci $folderB) -Property Name, LastWriteTime |
% { 
  if ($files[$_.Name] -eq $null) {
    $files[$_.Name] = New-Object PSObject -Property @{FolderA = ''; FolderB = ''; TimeStampA = $null; TimeStampB = $null}
  }
  if ($_.SideIndicator -eq "=>") {
    $files[$_.Name].FolderA    = $folderA + '\' + $_.Name
    $files[$_.Name].TimeStampA = $_.LastWriteTime
  } else {
    $files[$_.Name].FolderB    = $folderB + '\' + $_.Name
    $files[$_.Name].TimeStampB = $_.LastWriteTime
  }
}
$files.GetEnumerator() | select -ExpandProperty Value | select FolderA, TimeStampA, FolderB, TimeStampB

Open in new window

But, honestly, I for myself would prefer an output looking like
filename   inA   TimeStampA            inB    TimeStampB
--------   ---   ----------            ----   ------------
tst1        x    01/01/2000 01:02:03   
tst2        x    01/01/2000 01:02:03    x     01/02/2000 03:04:05

Open in new window

as repeating the file path for each file does not add any information.
0
 
Mach03Author Commented:
Qlemo, you are amazing!  That scripts works way better than I ever could have imagined!

Thank you!  Thank you!
0
 
Mach03Author Commented:
Perfect!
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now