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.
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
···
On Thu, Jan 20, 2011 at 9:48 AM, Mikhail Slyusarev <slyusarevmikhail@gmail.com> wrote:
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.
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
···
On Thu, Jan 20, 2011 at 7:55 PM, Mikhail Slyusarev <slyusarevmikhail@gmail.com> wrote: