Using the iTunes SDK to Remove Duplicate Songs.
I previously introduced you to the iTunes for Windows SDK in a previous article. As I mentioned in the previous article, I had just received my iPod Nano and came to the realization that my iTunes library was out of control. I discovered the iTunes SDK and the sample scripts it included. I was especially pleased with the RemoveDeadTracks script that deleted all the orphaned song entries that were left after I deleted the songs outside of the iTunes interface. I still had a problem with duplicate tracks, and decided it was the perfect oportunity to use the SDK to automate the deletion of these duplicate tracks. After spending a little time getting acquainted with the SDK I broke out my favorite script editor (windows notepad) and started to get to work.
With this script my goal was to:
- Delete duplicate tracks using the assumption that if the song title and the album was the same it was a duplicate
- Keep the song that had been recorded at the higher bit rate or was the largest file size (bigger is better… right?).
- Delete the file from the file system as well as the iTunes library.
Let’s take a look at the working portion of the script (to download the complete script use this link).
First we create the initial objects we are going to use in our script. In this section we create a File System Object and the iTunes objects.
Set fso = CreateObject(”Scripting.FileSystemObject”)
Set objApp = CreateObject(”iTunes.Application”)
Set objLibrary = objApp.LibraryPlaylist
Set colTracks = objLibrary.Tracks
We then loop through all the tracks..
For Each objTrack in colTracks
Do a search for songs with the same name
Set colTracksSrch = _
objLibrary.Search(objTrack.Name, ITPlaylistSearchFieldSongNames)
We check to see if more than one song by the same name is found in the search and loop through all the search results and see if the Song Names and Albums are the same. (checking for the duplicate files)
if colTracksSrch.count > 1 then
For Each objTrackSrch in colTracksSrch
if (objTrack.Name = objTrackSrch.Name) and _
(objTrack.Album = objTrackSrch.Album) then
If we find a duplicate using our criteria from above, we want check to see if the quality or size of the original is the less than the file we are comparing it to from our search results. If so, delete the file using the File System Object, remove it from the iTunes library and go to the next song.
if (objTrack.SampleRate < objTrackSrch.SampleRate) OR _
(objTrack.Size < objTrackSrch.Size) then
if fso.fileexists(objTrack.Location)then
fso.deletefile objTrack.Location, True
end if
objtrack.delete
Exit For
end if
Our next test is to see if we truly have a duplicate with all the same atributes with the exception of the file name. We then procede to delete all the duplicate files found in the search, keeping the original file.
if (objTrack.SampleRate = objTrackSrch.SampleRate) and _
(objTrack.Size = objTrackSrch.Size) AND _
(objTrack.Location <> objTrackSrch.Location) then
if fso.fileexists(objTrackSrch.Location) then
fso.deletefile objTrackSrch.Location, True
end if
objtrackSrch.delete
end if
The working part of the script is done, we now return to our loop and look at the next song in the list and repeat.
end if
next
end if
set objTrackSrch = nothing
Set colTracksSrch = nothing
Next
At this point everything is done. We notify the user the process is done and destroy the objects we used, releasing memory back to the Operating System.
msgbox (”Done Deleting Duplicates”)
‘Destroy the objects
Set objApp = nothing
set objTrack = nothing
Set objLibrary = nothing
Set colTracks = nothing
set fso = nothing
Scripting iTunes could never be so easy. You can download the script here. If you find this script helpful or have a suggestion to enhance the script, please leave me a comment.
remove_dup_itunes_tracks.zip
Please be aware that this script deletes files without prompting you.
If you are uneasy about running this script, backup your
music before proceeding.
Update 8/27/2006.. Version 1.1 released. Includes logging.
Technorati Tags: iTunes, Scripting, VBScript
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
Comments
nice job,
the first tool(script) i found for windows that does get rid of these unwanted duplicats on itunes.
the only problem i have is that after a while i get the following errormessage:
script: Remove Dup iTunes Tracks
row:33
character: 5
error: object missing ‘colTrachsSrch’
code: 800A01A8
source: runtime error Microsoft VBScript
(i translated this form german and hpoe it makes sense to you)
would be great if this could be fixed.
apart from that i have many duplicats with silghtly different spellings cause they are from different sources. i think it could be worth to look into how to delete them as well.
best, *e
I didn’t run across this error in my testing. I will post a version that introduces more logging in the next day.
Just curious.. Do you have a lot of untitled tracks in your Library?
well, i have about 500 tracks wich are just call “track 01″, “track 12″ and so on. if thats what you mean by untiteld tracks.
i have also some wich have the same name like “track 24″ and the same album name, case the album came on four cds. they only differ in their tracklengh. don’t know if you have included that criteria.
*e
Edzard…
After looking at your post earlier.. I think I see your problem.. You are referencing an object called “colTrachsSrch” wich should be “colTracksSrch” (the “h” should be a “k”). I would download the complete script using the link below and see if your results change.
hi jeff,
you were defenitly right about my misspelling. it should have been “colTracksSrch” , of course.
i downloaded your scipt from the link above but unfrotunately i am still getting the same error message after about ten minutes of search.
*e
E..
I just uploaded a new version of the program that has some loggin logic in it. Download the new version and review the log after it runs.. It should give you an idea on what song is causing the problem.
Jeff.
Hey,
so the script finds my duplicates well.
plenty of ***** Match Made ***** in my log files.
I ran it twice and renamed the first log.
But it doesn’t remove any of them, just finds them.
Any ideas?
thanks
A…
You will find a lot of the Match Made references in your log file. The way the search is handled in iTunes, it includes the song name that we are querying for. So the source file is always in the list of search tracks. Check your first log file for instances of “***** Deleted” to verify if any files were deleted. One thing to note is that the script is only designed to delete duplicates songs that share the same song and album name.
Jeff.
ehmm…
can you tel me where i can find the log file after the script was terminated by that runtime error?
*e
Agh. Such a fool not to backup my music library before doing this. USE CAUTION! Large swathes of my music library have been lost!
I think it is because in my library almost every track had a duplicate (for an unknown reason) and the 1st of the 2 duplicate entries in iTunes pointed to a nonexistent file… whereas the 2nd entry points to a real mp3 file.
So the script has compared 2 entries, and deleted the 2nd “duplicate”… which happens to be the REAL one. I don’t think it checks that both files actually exist. Maybe that could be added?
I now have a LOT of empty folders. Hopefully i can get all this back somehow… ![]()
Matt..
Sorry that you had problems with the script.. I can recommend the RemoveDeadTracks.js script that comes with the iTunes SDK ( http://www.treeratfishing.com/.....tunes-sdk/ )
I had already removed dead tracks before I ran this script on the machines I tested it with. You bring up a good point; I will add checking for dead tracks in the next version of the script.
Jeff.
Don’t worry Jeff - my fault not yours. And the script DID work. I just thought it was worth posting a note of caution!
I think i’m OK, as i didn’t sync the iPod afterwards, so i can just wipe the library and copy it all back over.
Would be good if it checked for dead tracks as well though.
Hi,
I was saying that it found alot so that you would know the search part is working and that it is actually looking at the data.
It does delete some files, but there are many more.
The are exact duplicates, same album, track, bitrate etc. here is an example of one without a deleted message from the log:
Rock-A-Bye Baby (Matches: 2)
Rock-A-Bye Baby The Corner Grocery Store
Rock-A-Bye Baby The Corner Grocery Store
***** Match Made *****
objTrack: 44100 1732355 C:\Documents and Settings\My Documents\My Music\Raffi\The Corner Grocery Store\3-17 Rock-A-Bye Baby 1.m4a
objTrackSrch: 44100 1732355 C:\Documents and Settings\My Documents\My Music\Raffi\The Corner Grocery Store\3-17 Rock-A-Bye Baby 1.m4a
Rock-A-Bye Baby The Corner Grocery Store
Rock-A-Bye Baby The Corner Grocery Store
***** Match Made *****
objTrack: 44100 1732355 C:\Documents and Settings\My Documents\My Music\Raffi\The Corner Grocery Store\3-17 Rock-A-Bye Baby 1.m4a
objTrackSrch: 44100 1732348 C:\Documents and Settings\My Documents\My Music\Raffi\The Corner Grocery Store\3-17 Rock-A-Bye Baby.m4a
but as you can see, it did not delete.
Any help would be appreciated.
if, as you said, it deletes the orphans first, this will be an awesomely useful tool!
thank you
A..
The first match is comparing values to the same file (because of the way the search works), so it shouldn’t be deleted. The second match is comparing different files but the script will only delete the TrackSrch file when it’s values are larger than the Track’s values. You should see an entry later on in the log where it is comparing the two songs when the “3-17 Rock-A-Bye Baby.m4a” is the Track and qualifies as a lesser quality song and is deleted.
You should see a new version of the code where it checks for orphaned entries in the next few days. I will also try to improve the logging with some better explanations.
Jeff.
[...] <!–adsense#button–>I looked at the iTunes SDK in “Scripting iTunes.. A look at Apple’s iTunes SDK” and I have shared with you a script I have wrote to eliminate duplicate song entries in your iTunes library in “Using the iTunes SDK to Remove Duplicate Songs“. I have used the feedback to help improve this script and am working on a way to remove the orphaned (or dead) song entries using VBScript. [...]
And if the quality of the two tracks is the same, it keeps both? There is no message deleting that track further down.
really appreciate the help.
If the quality of the two tracks are the same and the location (filename) is different it will delete the TrackSrch file. I do it this way because I am in the TrackSrch loop and want to compare to the next song in the search results. Unfortunately, I have broken this by having the “Exit For” on line 58 of the script. The script will still pick up these dupes as it goes through the library listing but it is not as effecient. I have updated the ZIP File
remove_dup_itunes_tracks.zip
It works perfectly. It amazes me how simple solutions can be and how complex people make them look like. Excellent work and thank you very much.
So I want to thank you for all your help.
This new script did start to delete more entries, then it errored with an invalid procedure call: line 36 char 5 code 800A0005.
It did that twice. there was nothing in the log.
A. It is strange that it has an error on a line that is supposed to log data.. Try splitting that line to the following lines. This should tell you which variable you are having a problem with.
objlog.WriteLine objTrack.Name
objlog.WriteLine “(Matches: ” & colTracksSrch.count & “)”
It very well could be that iTunes is having problems searching by the name of a particular song.
Jeff.
[...] This is a continuing look at what we can accomplish with the iTunes SDK. If you have missed the previous articles in this series please take a look at An introduction to Apple’s iTunes SDK, Removing Duplicate Songs, and Removing Orphaned Songs. [...]
hi jeff, i am getting that same error message as “A”: invalid procedure call: line 36 char 5 code 800A0005.
actually it is the error i was getting all along. only the line changed from 33 to 36 after you updated the script.
as i am no good in scripting, could you do the changes mentioned above and update the download by any chance.
i just want to mention that your script did work and deleted about 10MB of duplicate songs from my library. but now its not doing it anymore. only that error message is coming up after 10min. and i have about 1500 duplicates left.
Edzard..
I made a change to the logging and also started checking for orpaned songs. Let me know how this works for you.
Jeff.
Fantastic tool. Thanks for making it.
I agree it should be used with care.
I was getting a few errors thrown up at me with the latest file, so being a little bit of a geek and curious to learn why, I took a look.
There seems to be a ” ‘ ” comment-out missing on line 12 that reads checking for orphan files.
so it was throwing up a syntax error.
After that was fixed, it ran longer but then I got:
error 800a01b6
“Object doesn’t support this property or method: ‘location’
line 35 character 5
the line reads
objlog.WriteLine objTrack.Name & ” (” & objtrack.location & “)”
Is the [objtrack.location] supposed to be capitalized? to read [objTrack.Location]??
edit: I tried this with no success
Also, there are 2 more instances that “objTrack” has been written in all lowercase on lines 62 and 71.
So far i havent gotten past the error 800a01b6
I have to admit that I’m not a programmer, so dont shoot me down too hard if Im wrong.
Any suggestions?
Cheers
Craig
Craig..
In VBScript the case of the code generally doens’t matter. It is not as strict as some languages (perl is a good example). But I do think I know what the problem is. I wish I would have thought of it earlier. I wasn’t checking to see if the track returned was a file (as opposed to a CD or URL).
Download the script again and let me know what the results are..
Edzard.. I believe this will address your error also.
Hi Jeff
Thanks for the help.
New error is-
Line 47 char 2
Error Call was rejected by callee
Code 80010001
objlog.WriteLine ” (Matches: ” & colTracksSrch.count & “)”
Is colTracksSrch supposed to have one S ?
Cheers for the quick reply.
I lost all my tunes [along with everything on my data drive] last month when my PC dropped the partition and had a MBR upset.
I got my music back [3 times over] with ‘Easy recovery Pro’ so your tool has been invaluble. So far reducing my 20,000 file music library back down to a respectable 10,000, with another 500ish duplicates to go.
Thanks again for the great script.
Craig.
dear jeff,
i downloaded your zip file and then unzipped it to my desktop. the file is “Remove_Dup_iTunes_Tracks.vbs”. i double click on it and i get the following windows script host error:
Script: c:\documents and settings\rick\desktop\Remove_Dup_iTunes_Tracks.vbs
Line: 29
Char: 1
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
i’m not a programer and don’t know what to do from this point. can you help me out?
thanks,
rick
Hey,
I am using iTunes7. I tried the script and already on the line:
“Set objLibrary = objApp.LibraryPlaylist”
i’ll get the first error message:
library not registered!
Is there any dll i need to register that this script works?
Your help is really appreciated!
Markus
I get an error the object for the logfile doesn’t seem to exist?
Anyway i removed the lines where it logs wich files are matched and removed.. Now it works..
It’s been running for some minutes now and my library has shrunk from 79GB’s to about 60GB’s
saved me quite some time if i don’t post a pissed of message within the next 24 hours it worked for me! ![]()
there is a spelling error in there.. colTracksSrch colTrackSrch.. i think it is misspelled in the DIM section.. throws up an error after a couple of minutes (?) anyway fixed it and we’re back up and running ![]()
Hi Jeff
So at first the program ran great, but then I got this message
Line: 54
Char: 9
Error: Invalid procedure call or argument
Code: 800A0005
Source: Microsoft VBScript runtime error
I still have duplicates but I have no idea what to do, im not computer swavy at all. Any suggestions?
Thanks
Natasha.. There was a bad version of the script on the website that had a typo on that line. Try re-downloading the script and trying the process again.
Eric and Rick.. You are getting an error during the log file creation process. The log file is set to go to the root of the C:\ drive. I put it there because it is easy for most people to find. You are probably running as a restricted (non-admin) user and don’t have permissions to write to the root of the c:\ drive. Find the line:
Set objlog = fso.OpenTextFile(”c:\iTunesRemDup.log”,8, true)
Change the “c:\iTunesRemDup.log” to a path that you have permissions to . If you just take the “c:\” out, it should write it in the same location as the script.
Thanks for posting this script!
The script did great with my cd/mp3 tracks - everything is pretty again!
Any chance you could modify this script to also remove duplicate podcast entries?
Thanks again!
Ray
I am glad it worked for you. I hadn’t even thought about duplicate podcast files. It shouldn’t be that hard, let me see what I can find. I plan on doing some work on this script this weekend, maybe I can work that in.
Great work on this script. I got an error message on line 54 Charactar 9, Invalid procedure call or argument. Code: 800A005. I just restatred the script and it would continue deleting. Great stuff!
This is a great script. One suggestion is that maybe it could send the file to the recycling bin instead of permanently deleting…?
Or, since sending to the recycling bin may cause an “Are you sure?” prompt, perhaps it would make more sense to move all duplicate files to a “duplicates” folder awaiting deletion and then at the end, send the entire folder to the recycling bin… This will allow the user to choose whether to send all duplicates to the recycling bin with one prompt or they could just leave them in the “duplicates” folder for manually deleting or examining, etc.
Sorry for the multiple posts, but I also wonder how the script handles files over the network. Is this something you contemplated? I can see not wanting to delete a file if the duplicate resides on a different computer.
Hi! I really need to use this script… seems like great tool… I t doesn’t work for me though… I keep getting an error on line 39, Char 5 — Invalid Procedure Call Code: 800a0005 - Microsoft VBscript runtime error.
I read the other posts.. Is there a chance that the wrong version of the script is up again ?
Thanks in advance!
Thanks to this posting i downloaded the SDK and ran the orphan files script. It work well, although i only had about 40 orphans. Next, i attempted running the Remove_Dup script. i recieve the error on LINE 54/Char9 “invalid proceedure call or argument” (Code 800A0005). I am a budding scripter so this is still outside of my capabilities. If it helps, I am running this on a WinXP Pro machine against an external USB HDD with about 24,000 songs.
Any help would be greatly appreciated, great work!
Hi Jeff,
First, please accept my apology in advance if this forum is the wrong place to contact you about this…..I am a novice!
I downloaded the script to delete duplicate songs from the itunes library. When I attempted to open the zip file I got a “Windows script host box notice”. The info in the box was as follows.
Line:39
Char:5
Error: Invalid procedure call or argument
Code:800A0005
Source:Microsoft VB Script routine error
I also downloaded the orphaned song file and it opened and worked successfully, in as much as it returned a box stating that there were no orphaned songs found.
Ben and Mark..
The line of code that you guys are erroring out on is a line that enters a line into the log file. From that I am guessing that you have an object (song) that doesn’t have a SampleRate, Size, or Location. I am curious to know if you have any movies in your iTunes library. The script shold only check songs, but it may be trying to check a movie file which wouldn’t have a sample rate.
Ben and Mark.. The previous entry was for Smann.
You guys are most likely having an issue with a song that has a blank name. I first saw this with a track off the Panic Chanel CD. It ripped with a blank name. Sort your library tracks by name and change the names of any tracks that don’t have a song title.
Hi Jeff
I’m in the process of converting from AAC to MP3 and of course I have ended up with thousands of dupilcates, is there anyway that the script can be altered to delete the duplicate AAC files only? as the MP3 file size is smaller and quality levles are the same.
You would be a real live saver.
Looks like a great script.
Mike
Mike… Since you have access to the filename of each song, it should be possible. By looking at the last 3 characters of the song filename you can distinguish between a AAC and a MP3 file.
Hi Jeff,
Great Util. Just one qualm with it is that for some reason it doesnt delete all the duplicates on the first run. I had to run it almost 5 times to get all the duplicates. Is there a limit per run or something?
Thanks,
Sumit
Peter.. The VBS files contained in the ZIP file are the script. You can put the file anywhere you want, I am sure a lot of people put them on the desktop and just run it from there. You shouldn’t have to modify the file unless you want to make changes.
Sumit.. I am not sure why it didn’t delete all the duplicates. My first guess is that you might have more than one duplicate of some of your songs. The fun thing about doing programming like this is that everybody’s iTunes library is different.
I ran across this script via a Yahoo! search but I’m not sure it will cure my problem. What I have run across with iTunes is that I have duplicate entries for MANY songs, but the duplicate entries point back to the SAME file on the hard drive… is this script, since it doesn’t prompt before deletion, going to delete the file the duplicate entries reference? All I want it to do is remove the entry from the iTunes library, not actually delete any files. Thanks!
John
Hi downloaded you nice script and it work little time… i have 23800 music songs in my computer and in the end of the search it displays me this messages:
Script: C:\Users\KozzyVizzy\Desktop\Remove_Dup_iTunes_Tracks.vbs
Line:47
Char:2
Error:Object required
Code:800A01A8
Source:Microsoft VBScript Runtime Error
Please E.mail me..
Sry my bad english.. ;D
Hi
Just a note from this Vista user, I had to edit the script to use c:\users\ because the script didn’t have access to c:\
Easy fix on line 29!
Cheers,
Dylan
Hi, I have music files in Apple Lossless and MP3 and would only like to remove dups of the same encoding type. In other words if I have 2 MP3 files with the same song name and album name, one of them should be deleted but if I have one copy of the song/album in m4a and a dup in MP3, that’s ok. I’ve been looking at the script but object programming arrived after the programming part of my career was over. Can you help with the syntax to make this change?
Never mind, I wrote a REXX program to read the XML file and delete the duplicates. It’s not as elegant as your solution but it got the job done.
Steve.. Sorry I didn’t get back to you in time, I have had my head in another project. I am glad you found a solution although I don’t know how elagant my solution is. I haven’t really done anything on it in about a year. I am toying with the idea of re-doing in in VB.net. I have a lot more flexibility in that environment.. –jeff.
DuckMan.. Thanks for the tip, I will try to keep that in mind as I am toying with the idea of re-writing the script in VB.net.
I have same problem as rick and mark in entry no. 48
Line:39
Char:5
Error: Invalid procedure call or argument
Code:800A0005
Source:Microsoft VB Script routine error
I do not have any files without file name. I wonder what other possibility it could be?
Jeff,
Thanks for this, works like a charm.
I modified it to move instead of delete. Email me and I’ll send it to you.
jhgkak at@ gmail dot. com
I see that you have had this page growing for some time, and I really appreciate all the developers in the world who take time to improve the quality of our “less than geniuses” computing experience - often asking nothing in return! THANK YOU! Not sure if you have time but, I try to run the script on a new VISTA machine (ugh)and get this:
script: c:\Users\Evelyn\Desktop\Remove_Dup_iTunes_tracks.vbs
Line: 29
Char: 1
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
How do I fix permissions so I can run this and reap the benefits of clearing out 2000 duplicates!
Thanks.
Evelyn..
This is probably due to the script writing a the log file to a place that you don’t have NTFS rights. Look for that line in the script and change it location (like your “my documents folder”) and re-run the script.
I have the same problem as evelyn. “I see that you have had this page growing for some time, and I really appreciate all the developers in the world who take time to improve the quality of our “less than geniuses” computing experience - often asking nothing in return! THANK YOU! Not sure if you have time but, I try to run the script on a new VISTA machine (ugh)and get this:
script: c:\Users\Evelyn\Desktop\Remove_Dup_iTunes_tracks.vbs
Line: 29
Char: 1
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
How do I fix permissions so I can run this and reap the benefits of clearing out 2000 duplicates!
Thanks.
”
I’m not sure what lines i need to change and what to change them to. Thanks so much if you have time
oh i figured it out, it was because i had already clicked it once and it was already running
oops.. thax neway guys

This article has been Dugg! http://digg.com/apple/Using_th.....cate_Songs