sorting songs to directories

M

Mikhail Slyusarev

I've got a bunch of music that is in one directory and I wanted to sort
it so I wrote a ruby program to do it. The program basically works, just
for some reason it misses a few files, throwing TagLib2's 'BadFile'
exception. However, if I run it twice (just sorting the files it missed)
it will sort those files correctly. There are more details in the
comments of the attached file.

Help would be sincerely appreciated. Thank you.

Attachments:
http://www.ruby-forum.com/attachment/5766/main.rb
 
R

Robert Klemme

I've got a bunch of music that is in one directory and I wanted to sort
it so I wrote a ruby program to do it. The program basically works, just
for some reason it misses a few files, throwing TagLib2's 'BadFile'
exception. However, if I run it twice (just sorting the files it missed)
it will sort those files correctly. There are more details in the
comments of the attached file.

Help would be sincerely appreciated. Thank you.

Attachments:
http://www.ruby-forum.com/attachment/5766/main.rb

Just a quick check but your regexp for matching songs in line 21 is
broken (you don't anchor so it will match anywhere). Also you don't
need a capturing group since you don't do anything with the matched
portion. This should be better:

elsif /\.(?:mp3|mp4|wma|flac|ogg|ape|wav|vox|aac|m4p|3gp|m4a)\z/i =~ song

Few more remarks.

Line 16: don't do that, it's hard to read and easy to miss that the
last one is actually a Hash.

Line 29: if you make "artists" a Set or Hash you don't need this and
it's more efficient.

The whole approach seems overly complicated to me. I'd rather do

UNKNOWN = "Unknown Type".freeze

workingDir = ...
Dir.chdir workingDir

Dir["*"].each do |f|
next if File.directory? f

dir = TagLib2::File.new(f).artist rescue UNKNOWN

# deal with the case that there is a non directory
# with this name
until File.directory? dir
Dir.mkdir dir rescue (dir << "_")
end

FileUtils.mv f, dir
end

Isn't this basically what your script does?

Kind regards

robert
 
M

Mikhail Slyusarev

pretty much, yes (i'm just learning ruby).

but this doesn't address the issue
 
R

Robert Klemme

pretty much, yes (i'm just learning ruby).

but this doesn't address the issue

Well, there isn't much online docs for TagLib2. There are two
possible explanations

- You don't use the lib properly (e.g. by not closing TagLib2::File
that you create).

- There is a bug in the lib.

Personally I'd create a simple test which ensures repeated reading of
tag files yields identical results, e.g.

Dir["**/*.{mp3,mp4,wma,flac,ogg,ape,wav,vox,aac,m4p,3gp,m4a}"].each do |f|
5.times do |i|
begin
tlf = TagLib2::File.new(f)
p tlf
tlf.close
rescue Exception => e
printf "Attempt %3d: Exception for file %s: %p\n", i, f, e
end
end
end

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top