sorting songs to directories

Discussion in 'Ruby' started by Mikhail Slyusarev, Jan 20, 2011.

  1. 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.

    Mikhail Slyusarev, Jan 20, 2011
    1. Advertisements

  2. 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 f

    dir = rescue UNKNOWN

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

    Isn't this basically what your script does?

    Kind regards

    Robert Klemme, Jan 20, 2011
    1. Advertisements

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

    but this doesn't address the issue
    Mikhail Slyusarev, Jan 20, 2011
  4. 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|
    tlf =
    p tlf
    rescue Exception => e
    printf "Attempt %3d: Exception for file %s: %p\n", i, f, e

    Kind regards

    Robert Klemme, Jan 21, 2011
  5. thanks, i think the closing thing was what was wrong.
    Mikhail Slyusarev, Jan 21, 2011
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.