In part one of my series on scripting iTunes using C# I showed you how to remove dead tracks from your iTunes library. Now let’s take it one step further and get rid of all those duplicate tracks you might have. For my purposes, I determined a duplicate track to be any track that has the same artist, album and track name as another track in the library. When I find a duplicate, I want to keep the track with a higher bitrate. iTunes will show you a list of the duplicates in your library, but I’m much to lazy to sort out what should be deleted and what shouldn’t.
I used a Dictionary object to store the tracks I checked with the key being the track name. If a matching name is found, then I dig a little deeper to verify that a duplicate has actually been found, then I keep the one with a higher bitrate.
Download the VS2008 C# Express project here.
Here’s a code sample:
//create a reference to iTunes
iTunesAppClass iTunes = new iTunesAppClass();//get a reference to the collection of all tracks
IITTrackCollection tracks = iTunes.LibraryPlaylist.Tracks;int trackCount = tracks.Count;
Dictionary<string, IITTrack> trackCollection = new Dictionary<string, IITTrack>();
ArrayList tracksToRemove = new ArrayList();for (int i = trackCount; i > 0; i–)
{
if (tracks[i].Kind == ITTrackKind.ITTrackKindFile)
{
string trackKey = tracks[i].Name + tracks[i].Artist + tracks[i].Album;if (!trackCollection.ContainsKey(trackKey))
{
trackCollection.Add(trackKey, tracks[i]);
}
else
{
if (trackCollection[trackKey].Album != tracks[i].Album ||
trackCollection[trackKey].Artist != tracks[i].Artist)
{
trackCollection.Add(trackKey, tracks[i]);
}
else if (trackCollection[trackKey].BitRate > tracks[i].BitRate)
{
tracksToRemove.Add(tracks[i]);
}
else
{
tracksToRemove.Add(tracks[i]);
}
}
}
}//now remove all duplicates
for (int i = 0; i < tracksToRemove.Count; i++)
{
tracksToRemove[i].Delete();
}





November 3rd, 2007 at 6:46 pm
I’m curious why you keep casting the fileTrack to an IITFileOrCDTrack before you add it to the ArrayList of tracks to be removed? track.Delete() is an IITTrack method.
November 3rd, 2007 at 9:24 pm
Thanks Joe. You’re right. When I wrote the code to remove duplicates, I just modified the code I had already written to find dead tracks. In that code I needed to cast to an IITFileOrCDTrack (to check the Location property), but I didn’t need to when deleting files. I’ll remove the unnecessary casting from the code snippet.
June 5th, 2008 at 5:07 am
Hi,
do you know if is possible to not open an instance of iTunes when I use the
iTunesAppClass iTunes = new iTunesAppClass();
I don’t want that my client opens an instance of iTunes.
Thanks a lot,
Sergio
June 5th, 2008 at 3:56 pm
Hello Sergio,
Unfortunately I don’t think there’s a way to use the iTunesAppClass without opening an instance of iTunes. At least I don’t know how to do it. Sorry.
September 13th, 2009 at 7:51 pm
Hi,
Do you have any idea if its possible to access the view options in itunes application?
for example adding another column or even just adding another menu item?
Thanks