Skip to content

Instantly share code, notes, and snippets.

@shiguruikai
Created July 10, 2020 00:15
Show Gist options
  • Save shiguruikai/27d3c8c14d64bae8b1c9df9824982142 to your computer and use it in GitHub Desktop.
Save shiguruikai/27d3c8c14d64bae8b1c9df9824982142 to your computer and use it in GitHub Desktop.
[CmdletBinding(DefaultParameterSetName = "default")]
param (
[Parameter(Mandatory = $true, Position = 0)]
[Parameter(ParameterSetName = "default")]
[alias("l")]
[string] $LeftPath,
[Parameter(Mandatory = $true, Position = 1)]
[Parameter(ParameterSetName = "default")]
[alias("r")]
[string] $RightPath,
[Parameter(ParameterSetName = "default")]
[switch] $Force,
[Parameter(ParameterSetName = "File")]
[switch] $File,
[Parameter(ParameterSetName = "Directory")]
[switch] $Directory
)
function Get-Hash([string] $Path, [string] $Algorithm = "MD5") {
(Get-FileHash -LiteralPath $Path -Algorithm $Algorithm).Hash
}
function Get-Items([string] $Path) {
Get-ChildItem -LiteralPath $Path -Recurse -Force:$Force -File:$File -Directory:$Directory
}
function Get-ItemsWithHash([string] $Path) {
try {
Push-Location $Path
Get-Items $Path | Select-Object ("FullName", "Length",
@{n = "RelativePath"; e = { Resolve-Path $_.FullName -Relative } },
@{n = "Hash"; e = { Get-Hash $_.FullName } })
}
finally {
Pop-Location
}
}
function Compare-Directory {
[CmdletBinding()]
param (
[parameter(Mandatory = $true, Position = 0)]
[alias("l")]
[string] $LeftPath,
[parameter(Mandatory = $true, Position = 1)]
[alias("r")]
[string] $RightPath
)
$LeftHash = Get-ItemsWithHash $LeftPath
$RightHash = Get-ItemsWithHash $RightPath
Compare-Object -ReferenceObject $LeftHash -DifferenceObject $RightHash -Property "RelativePath", "Hash"
}
filter Find-LeftSide { $_ | ? { $_.SideIndicator -eq "<=" } }
filter Find-RightSide { $_ | ? { $_.SideIndicator -eq "=>" } }
filter Select-RelativePath { $_ | Select-Object -ExpandProperty "RelativePath" }
$LeftHash = Get-ItemsWithHash $LeftPath
$RightHash = Get-ItemsWithHash $RightPath
Write-Host "ディレクトリ配下の全てのファイルの差分を表示"
[PSCustomObject]@{
"Side" = "Left"
"TotalCount" = $LeftHash.Count
"Directory" = Resolve-Path -LiteralPath $LeftPath
},
[PSCustomObject]@{
"Side" = "Right"
"TotalCount" = $RightHash.Count
"Directory" = Resolve-Path -LiteralPath $RightPath
} | Format-Table -AutoSize
$diff = Compare-Object -ReferenceObject $LeftHash -DifferenceObject $RightHash -Property "RelativePath", "Hash"
$leftDiff = $diff | Find-LeftSide
$rightDiff = $diff | Find-RightSide
$diff2 = Compare-Object @($leftDiff) @($rightDiff) -Property "RelativePath"
$leftOnly = $diff2 | Find-LeftSide | Select-RelativePath
$rightOnly = $diff2 | Find-RightSide | Select-RelativePath
$diffOnly = $diff | Select-RelativePath | Group-Object -CaseSensitive | ? { $_.Count -ge 2 } | Select-Object -ExpandProperty Name
Write-Host "`n`tDiff:`t$($diffOnly.Count)"
$diffOnly
Write-Host "`n`tLeft:`t$($leftOnly.Count)"
$leftOnly
Write-Host "`n`tRight:`t$($rightOnly.Count)"
$rightOnly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment