Removing Dead Tracks (Duplicates that don't exist) from iTunes using C#
I hate iTunes on Windows with the heat of a thousand suns. It is a pox on my existence and it has cost me hours of pain and suffering. I'm sure it's all unicorns and spun sugar on MAC, but as a guy using Windows with dozens of gigabytes of music on a shared file server that I've ripped since the first CD I ever purchased, it's hell on earth. I could manage all my music with Zune (and I do have a Zune Pass for leasing music I don't want to buy) but there are iDevices in the house and iTunes is what it takes.
Did I mention it sucks? It's slow, frustrating, and is effectively just a giant listbox that exists only to mess up my music library and occasionally try to sneak Safari and QuickTime onto my machines. But enough about Satan, let's yank some dead tracks, shall we?
At some point, I ended up with hundreds of dead tracks and/or duplicates. I ended up with "LL Cool J - I Need Love 1.mp3" and "LL Cool J - I Need Love 2.mp3" along side the original. It was taking up many gigs of duplicate space. I searched for *1.mp3 and *2.mp3, etc, and deleted the dupes on disk.
However, this then left me with a big iTunes database that THINKS it has music, even though the file on disk is long gone. Now, as a napalm-style solution, you CAN delete your iTunes library completely and re-add it. It'll add just files that exist, but you will lose any edits, artwork, etc, you may have added. This solution wasn't cool for me so I say, nay nay.
There's a MILLION stupid little shareware apps that purport to fix duplicates and remove dead tracks. In my case, removing dupes was easy, but yanking dead tracks isn't worth me spending money.
Instead, I sat down tonight and decided to write a script in C# that said something like "foreach track, does that track exist where the system thinks it should be? No? Delete it."
A little googling with Bing, however, brought me to a three year old post at ScarTech where Shawn (shame he's stopped blogging) did the code for me! Yay. Here's a bit of his code, using the iTunes COM SDK. His stuff was written using Visual Studio Express 2008, but I upgraded to 2010. Also, I'm on 64-bit and his COM code expects x86, so I changed the project properties from AnyCPU to x86 and it worked great.
//create a reference to iTunes
iTunesAppClass iTunes = new iTunesAppClass();
//get a reference to the collection of all tracks
IITTrackCollection tracks = iTunes.LibraryPlaylist.Tracks;
for (int i = trackCount; i > 0; i–)
{
IITTrack track = tracks[i];
if (track.Kind == ITTrackKind.ITTrackKindFile)
{
IITFileOrCDTrack fileTrack = (IITFileOrCDTrack)track;
//if the file doesn’t exist, we’ll delete it from iTunes
if (fileTrack.Location == String.Empty || !System.IO.File.Exists(fileTrack.Location))
{
fileTrack.Delete();
}
}
}
Here's before:
So I ran it...
...and here's the afterparty:
Looks like it checked 7219 tracks and removed 570 dead ones. Yum.
I'd also encourage you to check out the second part of Shawn's iTunes and C# tutorial series where he creates a duplicate finder that knows to keep the higher bitrate song.
You can download Shawn's code here but as he hasn't blogged in a LONG time, I've mirrored it at SkyDrive. Big thanks to Shawn at ScarTech for saving me my evening and for tidying up my iTunes.
Good stuff, the internets.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
The problem with duplicate songs is that you can have the same song on two or more albums (eg I have "Sultans of Swing" on Money For Nothing, Sultans of Swing and Live at the BBC).
Why don't you remove your dead tracks and then spend an hour on the weekend cleaning up iTunes?
This script is great - I ended up doing mine on Windows by hand ...
"It'll add just files that exist, but you will lose any edits, artwork, etc, you may have added. This solution wasn't cool for me"
Oh what's that? You perform edits that you don't want to lose with this software you pretend to hate with the power of sums? That's adorable. It's always fun seeing someone discover platform lock-in for the first time.
All that talk about Zune is transparently fake (and invalidates your valid whinges, topping it with fanboism), maintain your music collection properly with metadata in the file instead of in proprietary databases you don't hit these messes.
Still have to use iTunes for updating my iPhone OS etc., but for music management I've never looked back.
Christian - I haven't done the math, but I feel like over 6000 tracks would take over and hour to deal with. That's 100 tracks a minute. Maybe it's possible.
You can't share libraries easily !!! this means my wifes login and mine has a different library and playlists and counts etc, this is poor it is time consuming and it annoys my wife intently. I don't want to turn into a basher but the other area that annoys us intently is the management of tracks, if I import them they don't show up on my wifes list you have to add manually !!
NO !! even though you can have these tracks on multiple ipods that are owned by myself they lock it out of the profile presumably as a feature so you can have your own tracklists etc , surely I should be able to choose this...the dupliacte tracks and missing tracks and permission issues is also a symptom of this... Itunes deliberately makes it difficult to do this and I'm stuck with it :(
Heat of a thousand suns? pssh!
BTW if you have "I need love" I certainly hope you also have "I'm that type of guy" - perhaps the best LL Cool J effort known to date.
Then I set my music as read-only before importing the files into iTunes... and I keep my music outside of the "iTunes Music Folder," just to keep it at a safe distance.
Now, when that beautiful and elusive day comes that I can leave these iDevices/iTunes in my past, my music will already be properly tagged, and I will finally find joy.
thanks for the post. this helped me sort out the same problem on my laptop. What i would like know is, Has anyone tried SuperSync?
http://www.supersync.com/index.php It purportedly does the removal of broken links as well as syncing music across pc and gadgets. I haven't seen anything about the dups though so ymmv.
I have used a small app called Tag & Rename for years its been money well spent. http://www.softpointer.com/tr.htm
-1 JD - are you an Apple Fanboy per chance - I think Scotts reply is a fair answer to your comments.
Try it..you'll never go back!
Didn't you mean the other way around (changed x86 to AnyCPU)?
On a side not, my brother and I are going to start posting again on ScarTech. We plan on sharing some more tools and tricks we use to make our lives a little easier.
iTunes Toolkit (of which I am the author) is a free program that has a function for finding and either repairing or deleting dead tracks. There are options to use either of two strategies to find dead tracks. It can use the iTunes COM interface to iterate through each track--in the same manner as your sample code. Unfortunately, that can be slow for a large library. Alternatively, a faster mode is achieved by parsing the iTunes Library.xml file. This has the additional benefit of telling where the file was located the last time iTunes was able to find it. The program's optional repair function lets you examine the folder the dead track was located in. This is helpful if the file has been renamed, in which case you can navigate to the track and re-associate it with the dead track.
For duplicate tracks, I use iTunes' "Display Duplicates" menu item (on the File menu). It's true that it finds a lot of false positives (i.e. different songs with the same title). However, I can easily eyeball the list and put the real duplicates in a separate playlist. Then I use iTunes Toolkit to delete the duplicate tracks from iTunes and from my hard drive.
By the way, I like iTunes a lot. The Column Browser (Ctrl+B) makes it very easy to drill down to a particular set of tracks. Unlike some of your commenters, I admire iTunes' tagging capabilities. Mass tagging is a breeze and autocomplete helps insure consistency, which is especially important for composer and artist tags. I also like the ability to join tracks, which is especially useful for ripping the movements of classical pieces.
I found your blog while searching for an alternative to VS2010's horrible help system. Many thanks for that.
Mike
I've been looking for something to ease the itunes pain, as i've had my tracks nicely organised (or so I thought) on the filesystem, and its been workable for the last 10 years.
I've scanned over 500 albums, and so far its only failed to identify 2 tracks.
Its pretty damn good, and well worth a donation.
Cheers.
Rob
The only other issue is retagging large groups of files, and for that, Tag & Rename has been a savior.
http://blog.ctaggart.com/2010/08/itunes-via-f-interactive.html
Comments are closed.