param(
[Parameter(Mandatory=$true)]
[string]$FolderPath1,
[Parameter(Mandatory=$true)]
[string]$FolderPath2
)
Ensure paths end with backslash for consistent path handling
if (-not $FolderPath1.EndsWith('')) {
$FolderPath1 += ''
}
if (-not $FolderPath2.EndsWith('')) {
$FolderPath2 += ''
}
Get all files recursively from both directories
$files1 = Get-ChildItem -Path $FolderPath1 -Recurse -File
$files2 = Get-ChildItem -Path $FolderPath2 -Recurse -File
Create hashtables mapping relative file paths to full paths
$files1Rel = @{}
foreach ($file in $files1) {
$relativePath = $file.FullName.Substring($FolderPath1.Length)
$relativePath = $relativePath.TrimStart('')
$files1Rel[$relativePath] = $file.FullName
}
$files2Rel = @{}
foreach ($file in $files2) {
$relativePath = $file.FullName.Substring($FolderPath2.Length)
$relativePath = $relativePath.TrimStart('')
$files2Rel[$relativePath] = $file.FullName
}
Get all unique relative paths from both folders
$allRelativePaths = $files1Rel.Keys + $files2Rel.Keys | Sort-Object -Unique
Initialize arrays to hold comparison results
$missingInFolder1 = @()
$missingInFolder2 = @()
$filesDifferent = @()
$filesIdentical = @()
Compare files based on relative paths
Write-Host "Comparing files:" foreach ($relativePath in $allRelativePaths) { $file1Exists = $files1Rel.ContainsKey($relativePath) $file2Exists = $files2Rel.ContainsKey($relativePath) Write-Host "Comparing file: $relativePath"
if ($file1Exists -and $file2Exists) {
# Files exist in both folders, compare content
$file1Path = $files1Rel[$relativePath]
$file2Path = $files2Rel[$relativePath]
# Compare file sizes first for quick difference detection
$file1Info = Get-Item -Path $file1Path
$file2Info = Get-Item -Path $file2Path
if ($file1Info.Length -ne $file2Info.Length) {
$filesDifferent += $relativePath
} else {
# Compute file hashes to compare content
$hash1 = Get-FileHash -Path $file1Path -Algorithm MD5
$hash2 = Get-FileHash -Path $file2Path -Algorithm MD5
if ($hash1.Hash -eq $hash2.Hash) {
$filesIdentical += $relativePath
} else {
$filesDifferent += $relativePath
}
}
} elseif ($file1Exists -and -not $file2Exists) {
# File is missing in FolderPath2
$missingInFolder2 += $relativePath
} elseif ($file2Exists -and -not $file1Exists) {
# File is missing in FolderPath1
$missingInFolder1 += $relativePath
}
}
Output the comparison results
Write-Host "Files missing in $FolderPath1 (present in $FolderPath2):"
if ($missingInFolder1.Count -gt 0) {
$missingInFolder1 | ForEach-Object { Write-Host $_ }
} else {
Write-Host "None"
}
Write-Host "`nFiles missing in $FolderPath2 (present in $FolderPath1):"
if ($missingInFolder2.Count -gt 0) {
$missingInFolder2 | ForEach-Object { Write-Host $_ }
} else {
Write-Host "None"
}
Write-Host "`nFiles that are different:"
if ($filesDifferent.Count -gt 0) {
$filesDifferent | ForEach-Object { Write-Host $_ }
} else {
Write-Host "None"
}
Write-Host "`nFiles that are identical:"
if ($filesIdentical.Count -gt 0) {
$filesIdentical | ForEach-Object { Write-Host $_ }
} else {
Write-Host "None"
}