DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Changing Names Of MP3 Song Files (present In A Directory) Using Tag Information. [For Win32] - Ruby

11.10.2010
| 1720 views |
  • submit to reddit
        
require 'rubygems'
require 'id3lib'
 
# Load a tag from a file
# tag = ID3Lib::Tag.new(ARGV[0])
fnames = []
fpath = []
path = []
check = 0
num = 0
record  = `dir /b /S *.mp3`.chomp!
fnames = record.split("\n")
 
fnames.each { |p|
    fpath = p.to_s.split('\\')
    fpath.slice!(fpath.size-1)
    path << fpath.join('\\')
}
 
def fileTail (file, offset)
    f=File.new(file)
    f.seek(-offset,IO::SEEK_END)
    tag,title,artist,album,year,comment,genre=
    f.read.unpack "A3A30A30A30A4A30C"
    raise "No ID3 Info" if tag!='TAG'
    f.close
    return title
end
 
count = 0
while (count < fnames.size)
    title = fileTail("#{fnames[count]}", 128)
    File.rename("#{fnames[count]}", "#{path[count]}"+"\\"+"#{title}"+".mp3")
    count += 1
    puts "."
end
 
puts "Processed #{count} Files in all."