Created
September 26, 2020 19:19
-
-
Save houhoulis/3bad4116a40a3cbe3cf32d43f77af3ab to your computer and use it in GitHub Desktop.
Find dupes that Apple's "Image Capture.app" is creating when importing photos from my camera, & move to trash. Compare dupes to the previous dupe (or original) via md5.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest' | |
def filenames | |
Dir.entries('.').sort | |
end | |
def digest filename | |
Digest::MD5.hexdigest File.read(filename) | |
end | |
def md5_matches? file1, file2 | |
filenames.include?(file1) && | |
filenames.include?(file2) && | |
File.file?(file2) && | |
digest(file1) == digest(file2) | |
end | |
def delete_if_dupe number, dupes_dir = Dir.pwd | |
if dupes_dir | |
begin | |
Dir.chdir dupes_dir | |
rescue SystemCallError | |
puts "Pass in a legit directory. You passed in '#{dupes_dir}'." | |
return | |
end | |
end | |
if filenames.grep(/IMG_\d{4}\.JPG/).empty? | |
puts "No pics in here" | |
return | |
end | |
puts "There are #{filenames.count} files in here." | |
suspect_pattern = Regexp.new("IMG.* #{number}.JPG") | |
suspects = filenames.grep suspect_pattern | |
puts "There are #{suspects.count} suspects in here." | |
predecessor_for = ->(suspect) { | |
if number == 1 | |
# "IMG_0123 1.JPG" | |
suspect[0..7] + ".JPG" | |
else | |
suspect[0..8] + "#{number - 1}.JPG" | |
end | |
} | |
# `brew info trash`: https://hasseg.org/trash/ | |
survivors = 0 | |
suspects.each do |suspect| | |
predecessor = predecessor_for[suspect] | |
if md5_matches?(suspect, predecessor) | |
`trash "#{suspect}"` | |
else | |
survivors += 1 | |
puts "Whew! #{suspect} escapes... for now...." | |
end | |
end | |
puts "Now, there are #{filenames.count} files left. There were #{survivors} survivors." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment