Sunday, August 31, 2008

Removing dead tracks from iTunes for Windows

Man, don't you just love having to use iTunes to manage and sync your iPods, iTouches, iPhones and iEtc? And isn't it great when you want to add new tunes to your library, but not 'consolidate' that library into your iTunes folder (thus doubling your disk usage)? Ooh! And if you move stuff around, you end up with a bunch of dead tracks? Well, here's a little piece of VBScript that will remove all the dead tracks in iTunes for windows. For every track it finds in the library that the file is no longer there -- BLAMMO - removed from the library.

  1. Open iTunes and go to your MUSIC library. Press CTRL-A to select all tunes. Don't do anything else.
  2. Save the little piece of code below to a file on your disk, say "c:\removedead.vbs"
  3. run this command using the Start button and the run option: cscript c:\removedead.vbs
  4. Wait. Patiently. Depending on the number of tracks you have in your library, it could take a while.
  5. When done, it will report the number of tracks removed from the library.
Code snippet:

' RemoveDead.vbs
ITTrackKindFile = 1
deletedTracks = 0


set iTunesApp = WScript.CreateObject("iTunes.Application")
set mainLibrary = iTunesApp.LibraryPlaylist
set tracks = mainLibrary.Tracks


for each currTrack in tracks
' is this a file track?
if (currTrack.Kind = ITTrackKindFile) then
' yes, does it have an empty location?
if (currTrack.Location = "") then ' yes, delete it
currTrack.Delete()
deletedTracks = deletedTracks + 1
end if
end if
next


WScript.Echo "Removed " & deletedTracks & " dead track(s)."
' RemoveDead.vbs