Organizing MP3s based on metadata with TagLib# and IronPython

With about 11,000 MP3 files, I need to keep them organized – making sure the metadata is correct and arranging them into a logical folder structure. My first problem was that I had a single folder with hundreds of MP3s files that I purchased from Amazon but I wanted them all to be placed into folders depending on the Album Artist.

Fortunately this was made simple by a great library called TagLib#. The compile the library from the TagLib# source on github. Or, more simply just download precompiled binaries from here.

Below is the IronPython code I used to get this done. It isn’t particularly robust, but it did the job for me in just a few seconds.


import os
import sys
import clr
import System
import TagLib

folder = r"X:\music"
files= System.IO.Directory.GetFiles( folder, "*mp3" )
def fixname(s) :
    s = s.replace("\"","-")
    return s

for f in files :
    print f
    with TagLib.File.Create(f) as tagfile:
        artists = tagfile.Tag.AlbumArtists
        if (len(artists)<1) : continue
        print artist
        artist_folder = System.IO.Path.Combine(folder, fixname(artist) )
        if (not System.IO.Directory.Exists(artist_folder)) :
        dest_filename = System.IO.Path.Combine(artist_folder, System.IO.Path.GetFileName(f))
        print dest_filename
        System.IO.File.Move( f, dest_filename )        


Comments (0)

Skip to main content