# select mp3 files which have id3 tag
# 2009/11/22
require 'find'
require 'rubygems'
require 'mp3info'
require 'kconv'
require 'fileutils'
search_path = ARGV.shift
dest_path = ARGV.shift
Find.find(search_path) do |filename|
next unless filename.match(/\.(mp3|MP3)$/)
if FileTest.directory?(filename) then
print filename, "\n"
next
end
tmp = Array.new
begin
Mp3Info.open(filename) do |mp3|
tmp = [
mp3.tag.artist.to_s.tosjis,
mp3.tag.album.to_s.tosjis,
mp3.tag.title.to_s.tosjis,
mp3.tag.tracknum,
mp3.mpeg_version,
mp3.layer,
mp3.bitrate,
mp3.samplerate,
mp3.length,
FileTest.size?(filename),
filename
]
end
rescue
print $!, "\n"
next
end
if tmp[2].to_s.size > 0 then
begin
tracknum = sprintf("%02d", tmp[3].to_s)
rescue
tracknum = "00"
end
new_file = [tmp[0].to_s, tmp[1].to_s, tracknum, tmp[2].to_s].join(' - ') + '.mp3'
new_file.gsub!(/[\/:<>\\\*\?\"|]/, '_') if new_file.match(/[\/:<>\\\*\?\"|]/)
print dest_path + '\\' + new_file, "\n"
begin
FileUtils.mv(filename, dest_path + '/' + new_file)
rescue
print $!, "\n"
next
end
end
end