-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleaner.rb
executable file
·69 lines (52 loc) · 1.33 KB
/
cleaner.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
require 'find'
require 'fileutils'
# This script will attempt to clean up your ITunes library
# Proceed with caution, it will delete some of your files :)
def Cleaner(start_dir=".")
# Array that will hold the unique files
files = Array.new
dups = Array.new
# get a full file list
Dir.new(start_dir).each do |file|
unless ['.','..'].include? file
files.push(file)
end
end
# Find the duplicate matches
Matcher(files, dups)
if dups.length == 0
print "No dups found nothing to delete. "
else
print "The following matches where found: \n"
puts(dups)
print "\n"
end
Delete(dups)
end
# This function uses the matching rules to find duplicates
# The duplicates are then put into the dup array to be deleted
def Matcher(files, dups)
for file in files
reg = /\d*\s*[\w+\s*]+\s+\d+.mp3/
if reg.match(file)
# Now that we have a match let's see if a base file exists
#first remove the extension
base = File.basename(file,".mp3");
reg_base = /\s*[a-zA-Z]+/
if (base_match = reg_base.match(base))
# We found a base file, so we can remove the other one..
base_match = base_match[0].strip
print "Base file: "
dups.push(file)
end
end
end
end
#Delete all the dupes from the file system
def Delete(dups)
for file in dups
File.delete(file);
end
end
Cleaner()