Skip to content

Instantly share code, notes, and snippets.

@henno
Created April 17, 2023 17:54
Show Gist options
  • Save henno/27bf6a091c5d6f2dc84299aadfb78886 to your computer and use it in GitHub Desktop.
Save henno/27bf6a091c5d6f2dc84299aadfb78886 to your computer and use it in GitHub Desktop.
Get-ChildItem -Recurse | Where-Object { $_.PSIsContainer } | ForEach-Object {
if (Test-Path $_.FullName) {
try {
Set-Location -LiteralPath $_.FullName
$files = Get-ChildItem -Path ./* -include *.jpg | Where-Object { $_.Length -gt 0 }
if ($files.Count -gt 1) {
$groups = $files | Group-Object { $_.Length }
$groups | ForEach-Object {
$group = $_.Group
if ($group.Count -gt 1) {
Write-Host "Found a group of files with the same file size"
$filesToKeep = @()
$group | ForEach-Object {
$file = $_
if ($filesToKeep.Count -eq 0) {
Write-Host "Adding first file to the files to keep list: $file.Name"
$filesToKeep += $file
}
else {
$resolution = & "identify" "-format" "%wx%h" $file.FullName
$duplicate = $filesToKeep | Where-Object {
$_.Length -eq $file.Length -and
$_.LastWriteTime -eq $file.LastWriteTime -and
(
& "identify" "-format" "%wx%h" $_.FullName
) -eq $resolution
}
if ($duplicate) {
Write-Host "Found duplicate file: $file.Name, deleting it"
#$file.Delete()
}
else {
Write-Host "Adding file to the files to keep list: $file.Name"
$filesToKeep += $file
}
}
}
}
}
}
Set-Location '..'
}
catch {
Write-Host "Error occurred: $_"
}
}
else {
Write-Host "Directory does not exist: $($_.FullName)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment