Archive

Posts Tagged ‘iTunes’

How to query your iTunes library

March 14, 2010 2 comments

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.

Advertisements
Categories: Development Tags: