Home > Development > How to query your iTunes library

How to query your iTunes library

As much as I hate iTunes its pretty much a necessary evil if you have a iPod or iPhone. One of the biggest problems I have is when a track is moved, there is no way to automatically locate the track, its just marked as missing and there is not much you can do about it other than locate it manually.

I had a browse today and came across a suggestion on Digg for using the WSH and the COM components to find tracks in the library and delete them. I decided to port this over to c# so we could really query the library. Here’s how I did it.

In a Visual Studio project, add a reference to the iTunes type library then create a class and add a method with the following code

public IEnumerable<IITFileOrCDTrack> GetITunesTracks()
    foreach (IITFileOrCDTrack track in new iTunesAppClass().LibraryPlaylist.Tracks)
        if (track.Kind == ITTrackKind.ITTrackKindFile)
            yield return track;

we now have a query object holding the entire media library. This gives us the ability to find all the tracks in the iTunes library which are missing by writing a simple method such as

public IEnumerable<IITFileOrCDTrack> GetITunesTracksWithoutALocation()
    return from track in GetITunesTracks()
    where string.IsNullOrEmpty(track.Location)
    select track;

the track object has a delete method which means we can just call delete if we wanted to remove all the missing items from your media library.

This is a quick and easy way to delete lost items, but could also be used for deleting podcasts older than a certain period of time, and probably many other things, hope you find it useful. I can make the source available if anyone needs it.

Categories: Development Tags:
  1. paul davies
    July 22, 2010 at 10:38 am

    Hi Kevin,
    Just found your blog. I like it, especially this article – will have to give it a go myself. You pleased to have hodgson at ‘pool?

  2. Kev Hunter
    July 22, 2010 at 4:44 pm

    Hey Paul,

    thanks, I need to update it much more frequently, but struggle to fine the time. With regards to Hodgson, I was gutted at first but i am pretty happy with the way he is conducting himself, just hope he convince big names to join


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: