Skip to content

Instantly share code, notes, and snippets.

@stephenfuqua
Created June 4, 2018 03:15
Show Gist options
  • Save stephenfuqua/ca25feb0afe97606f31df870f7fc8b0d to your computer and use it in GitHub Desktop.
Save stephenfuqua/ca25feb0afe97606f31df870f7fc8b0d to your computer and use it in GitHub Desktop.
# After a botched Windows file history process a few years ago, many of my
# photos files ended up being duplicated (sometimes several times). This
# script hunts down duplicates by name and size and deletes the one
# with the longer name. Examples:
# IMG_2825.JPG and IMG_2825 (2015_03_26 01_58_35 UTC).JPG
# IMG_2199.JPG, IMG_2199-1.JPG, IMG_2199 (2014_10_13 02_31_59 UTC).JPG, IMG_2199-1 (2014_10_13 02_31_59 UTC).JPG
$startDir = "c:\temp\Photos"
(Get-ChildItem -Path $startDir -Recurse) | ForEach-Object {
$dir = $_.FullName
(Get-ChildItem -Path $dir -Filter "*.JPG") | Sort-Object -Descending | Where-Object {$_.Name.Length -eq 12} | ForEach-Object {
$duplicated = $_.Name
$duplicatedLength = $_.Length
Write-Host "Evaluating $duplicated..."
(Get-ChildItem -Path $dir -Filter "$($duplicated.Substring(0,8))*.JPG") | ForEach-Object {
# `-Exclude $duplicated` in the Get-ChildItem command isn't working -
# for some reason it excludes all files :-(
if ($_.Name -ne $duplicated -and $_.Length -eq $duplicatedLength) {
Write-Host "Delete $($_.Name)"
Remove-Item -Path $_.FullName -Force
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment