Solved

# Powershell that compairs folders

Posted on 2012-09-14
260 Views
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
Question by:Mach03

LVL 16

Expert Comment

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

LVL 67

Accepted Solution

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

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

as repeating the file path for each file does not add any information.
0

Author Comment

Qlemo, you are amazing!  That scripts works way better than I ever could have imagined!

Thank you!  Thank you!
0

Author Closing Comment

Perfect!
0

## Featured Post

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…