Compare two lists of files and their content Compare two lists of files and their content powershell powershell

Compare two lists of files and their content


You could do a checksum of each file and compare that...

$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($file)))


Tim Ferrill's idea for checking updated files seems like a much better way to compare the files. Do something like

$A = Get-ChildItem -Recurse -path "C:\repos\Dev\Projects\Bat\CurrentVersionRepoCloneTemp" -filter "*.config"$B = Get-ChildItem -Recurse -path "C:\repos\Dev\Projects\Bat\UpgradeVersionRepoCloneTemp" -filter "*.config"$A | %{$_ | Add-Member "MD5" ([System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($_))))}$B | %{$_ | Add-Member "MD5" ([System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($_))))}

Then I'd do the compare and group by directory.

$C = Compare-Object $A $B -Property ('Name', 'MD5') - Passthrough | Group Directory

After that, getting actual changes, that's going to be a little slow. Doing a line-by-line match of file contents is rough, but if they aren't too large it should still happen in a blink of an eye. I'd suggest something like:

$Output = @()ForEach($File in $C[1].Group){    $OldData = GC $File    $C[0].Group | ?{$_.Name -eq $File.Name} | %{        $NewData = GC $_        $UpdatedLines = $NewData | ?{$OldData -inotcontains $_}        $OldLines = $OldData | ?{$NewData -inotcontains $_}        $Output += New-Object PSObject -Property @{            UpdatedFile=$_.FullName            OriginalFile=$File.FullName            Changes=$UpdatedLines            Removed=$OldLines        }    }}

Once you have that you just have to output it in something readable. Maybe something like this:

Get-Date | Out-File "C:\repos\Dev\Projects\Bat\UpgradeVersionRepoCloneTemp\ChangeLog.txt"$Output|%{$_|FT OriginalFile,UpdatedFile; "New/Changed Lines"; "-----------------"; $_.Changes; " "; "Old/Removed Lines"; "-----------------"; $_.Removed} | Out-File "C:\repos\Dev\Projects\Bat\UpgradeVersionRepoCloneTemp\ChangeLog.txt" -Append