Last active
September 30, 2016 18:38
-
-
Save djk29a/a5310c011d8d3532614c372f02d3bb82 to your computer and use it in GitHub Desktop.
Remove duplicate files in iTunes as a result of Consolidate Files
This file contains 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 'find' | |
root = '/Volumes/music' # pick your path | |
Find.find(root) do |path| | |
if FileTest.directory? path and File.basename(path)[0] == '.' | |
Find.prune | |
else | |
d, f = File.split path | |
if f =~ /^(\d+(\-\d+)?.+)(\s\d+)\.(mp3)$/i | |
orig_file = File.join(d, $1 + '.' + $4) # check for existence of original file name including extension case | |
if File.readable? orig_file | |
puts "O: #{$1 + '.mp3'}, deleting #{f}..." | |
begin | |
File.unlink(path) | |
rescue | |
puts "Error deleting #{f} in #{d}, try later outside here" | |
end | |
end | |
end | |
end | |
end |
This file contains 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 'find' | |
require 'fileutils' | |
root = '/Volumes/music' # pick your path | |
Find.find(root) do |path| | |
if FileTest.directory? path | |
if File.basename(path)[0] == '.' | |
Find.prune | |
else | |
next | |
end | |
else | |
# look for folder.jpg / cover.jpg | |
if path =~ /(.jpg)$/i | |
dir, filename = File.split path | |
basedir, albumdir = File.split dir | |
if albumdir =~ /^(?:\d+\s\-\s)(.+)/ | |
itunes_dname = File.join(basedir, $1) | |
puts "Checking for directory #{itunes_dname}: " | |
if File.directory? itunes_dname and File.readable? itunes_dname | |
puts "\e[32mOK\e[0m" | |
tgt = File.join(itunes_dname, filename) | |
if path == tgt | |
puts "Already in ok path. Skipping" | |
else | |
puts "Moving: #{path} to #{tgt}" | |
FileUtils.mv(path, File.join(itunes_dname, filename)) | |
end | |
else | |
puts "\e[31mNOK\e[0m" | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And if it's not obvious, this will totally break any file references that iTunes or other programs would have to the deleted files.