K
Khurrum Ma
I'm a RubyNoobie and I am writing a few random scripts to learn the
language. Would it be alright to post scripts to get them criticized by
pros?
This is supposed to search a folder to find duplicate MP3s and move them
to a destination directory.
*When you search a directory structure with lots of branches then
mp3info gives an error.
*The source and destination folders NEED to have the backslash \ or else
the script does horrible things. So it needs better input checking
I don't always know the best programmer practices and it would help me
if someone can make suggestions.
# Finds duplicate MP3s and move them to stated destination
# usage: duplicatemp3.rb c:\source\dir\ c:\destination\dir\
# the ending \ slash is REQUIRED
require "mp3info"
require "ftools"
class DuplicateMP3
def initialize()
unless ARGV.length == 2
puts "usage: duplicatemp3.rb c:\source\dir\ c:\destination\dir\"
exit
end
@dir_source = ARGV[0]
@dir_destination = ARGV[1]
showallduplicates()
end
def showallduplicates()
directory = @dir_source
duplicates = Hash.new { |h,k| h[k] = [] }
Dir.chdir(@dir_source)
#Check if the file is a file then
#Make a unique ID with the mp3 title and mp3 song length combined
#Enter unique into the hash as a key
#If unique key is already in the hash then move the file to destination
folder
Dir["**/*.mp3"].each do |file|
if File.file?(file)
Mp3Info.open(file) do |mp3|
unique = mp3.tag.title.to_s + mp3.length.to_s
if (duplicates.has_key?(unique))
puts "duplicate: #{file}. Moving..."
movedupes(file)
else
duplicates[unique].push(file)
end
end
end
end
end
def movedupes(file)
source = @dir_source + file.to_s
dest = @dir_destination + File.basename(file.to_s)
File.move(source, dest)
puts "file moved"
end
end
find = DuplicateMP3.new()
language. Would it be alright to post scripts to get them criticized by
pros?
This is supposed to search a folder to find duplicate MP3s and move them
to a destination directory.
*When you search a directory structure with lots of branches then
mp3info gives an error.
*The source and destination folders NEED to have the backslash \ or else
the script does horrible things. So it needs better input checking
I don't always know the best programmer practices and it would help me
if someone can make suggestions.
# Finds duplicate MP3s and move them to stated destination
# usage: duplicatemp3.rb c:\source\dir\ c:\destination\dir\
# the ending \ slash is REQUIRED
require "mp3info"
require "ftools"
class DuplicateMP3
def initialize()
unless ARGV.length == 2
puts "usage: duplicatemp3.rb c:\source\dir\ c:\destination\dir\"
exit
end
@dir_source = ARGV[0]
@dir_destination = ARGV[1]
showallduplicates()
end
def showallduplicates()
directory = @dir_source
duplicates = Hash.new { |h,k| h[k] = [] }
Dir.chdir(@dir_source)
#Check if the file is a file then
#Make a unique ID with the mp3 title and mp3 song length combined
#Enter unique into the hash as a key
#If unique key is already in the hash then move the file to destination
folder
Dir["**/*.mp3"].each do |file|
if File.file?(file)
Mp3Info.open(file) do |mp3|
unique = mp3.tag.title.to_s + mp3.length.to_s
if (duplicates.has_key?(unique))
puts "duplicate: #{file}. Moving..."
movedupes(file)
else
duplicates[unique].push(file)
end
end
end
end
end
def movedupes(file)
source = @dir_source + file.to_s
dest = @dir_destination + File.basename(file.to_s)
File.move(source, dest)
puts "file moved"
end
end
find = DuplicateMP3.new()