Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently consolidated my iTunes Library and now many videos have a "1" at the end of the file name. And weirdly, in the media library, there are two files - one with the "1" and one without. Luckily only one video appears in iTunes. Is there some way to clean up my media folder AND the filenames in itunes?

share|improve this question
And in some cases, iTunes put a "2" after the file name in the library. By "1" and "2" I mean that there is a space, and then a 1 or 2. Kind of like when you add a file to a folder and the Finder asks if you wish to keep both, it adds a number. – isaunders501 Mar 16 '13 at 16:55
Potential solutions exist but it heavily depends on the number of impacted files. Are we talking about a handful or hundreds? – patrix Mar 16 '13 at 19:32

I just use a quick and dirty AppleScript to do things like this:`

-- BP Jan 2014
-- Trims a set number of characters from the Start of track names in an iTunes playlist
-- Or trims a set number of characters from the End of track names in a playlist
-- Or adds a prefix to each track name in a playlist.
-- Or adds a suffix to each track name in a playlist.
-- Trims take precedence over adds
-- It's best to do just one operation at a time, and set all the other variables to "" or 0 as appropriate.
--------------------------------- User settable variables
set PlaylistName to "AAA" -- Just add the tracks you want to change to a single playlist. When dione, delete the playlist.
set trimInitialchars to 0 -- nonzero value takes precedence over any addstart value
set trimFinalchars to 0
set addstart to "" -- nonempty value wil overide addend
set addend to ""
--------------------------------- End of user settable variables
set trimstart to trimInitialchars + 1
tell application "iTunes"
    set tracklst to every track of playlist PlaylistName
    set trackcount to number of items in tracklst
    repeat with n from 1 to trackcount -- Do the whole playlist:
        set oldtitle to name of track named (name of item n of tracklst)
        if trimInitialchars is greater than 0 then -- Trim stuff from front of track names
            set name of (item n of tracklst) to (characters trimstart through length of oldtitle) as text
        else if trimFinalchars is greater than 0 then -- Trim stuff from front of track names
            set name of (item n of tracklst) to (characters 1 through ((length of oldtitle) - trimFinalchars) of oldtitle) as text
        else if addstart is not "" then -- Add something to front of track names
            set name of (item n of tracklst) to (addstart & oldtitle)
        else if addend is not "" then -- Add something to end of track names
            set name of (item n of tracklst) to (oldtitle & addend)
        end if
    end repeat
end tell`
share|improve this answer

The most possible issue is you already have added songs (with copying to iTunes folder, it's by default), removed them with 'keep files' variant.

following steps should fix this:

  1. Remove weird titles from iTunes library and quit from.
  2. open your iTunes media folder ~/Music/iTunes/iTunes Media/Videos (you can find it in preferences)
  3. remove weird titles from this folder (you can save correct file copies if you have no backup)
  4. Also you should remove these entries from iTunes library.
  5. re-add correct versions into library.

UPDATE: added awk script and description for.

How to use this script:

  1. save it to the file named ~/dups.awk (~ is your home directory)
  2. open your and change directory to your iTunes media folder like we talked before (e.g. cd "~/Music/iTunes/iTunes Meda/Videos")
  3. remove duplicate files with command find $(pwd) -type f|sort -r|awk -f ~/dups.awk| xargs -L 1 rmtrash

some important notes:

  1. this script does respect unicode and spaces in file names. I've tested it.
  2. I believe that unix-way tools are enough to do this task.(they are the fastest too)
  3. rmtrash is utility from MacPorts, sends files to your Trash.
  4. rm command remove files directly from file system and they can't be restored from trash bin. This is the fastest and the best way if you really sure what do you do.
  5. using automated stuff like this I always double check if there's no mistakes in output. You can replace command xargs with arguments with less command or redirect output to the file (e.g. ~/dups.txt)
func checkfile(a,b)
    if (length(a)+2 < length(b) || a != substr(b, 1, length(a)))
        return 1;
    suffix = substr(b, length(a)+1);
    return !match(suffix, "^ [0-9]+$");

func cutext(a)
    return  substr(a, 1, length(a)-4);

    if (length(name) > 0)
        if (checkfile(name, file))
            name = file;
        name = file;
share|improve this answer
Thank you. We are talking hundreds, if not thousands, of files here. I was wondering if there was a program or script (like a doug's apple script) that would compare what itunes is displaying, with what is in the media folder - and ELIMINATE anything that's in the media folder that itunes isn't displaying. Or perhaps there is a script that looks to see if there is a " 1" or " 2" after the name in iTunes, and remove that extra 1 or 2? – isaunders501 Mar 16 '13 at 20:31
Right now, I think Eir Nym is the only way to go: delete files from library, then delete duplicates from media folder, and reimport all files. Problem is, I have nearly 10TB of files - more than 25000 songs and about 10,000 movies and TV shows. It would be an impossibly big task. – isaunders501 Mar 16 '13 at 20:35
I see. Please, wait just a little, I'll write you little script. – Eir Nym Mar 17 '13 at 4:25
Your script is here. – Eir Nym Mar 17 '13 at 6:32
WOW. OK Eir Nym. You are making me brave. I need to study this script a bit and then see if I am going to make it work. Just to be careful I think I am going to get help from a more confident Terminal junkie than me. On the plus side I do have my old library which I THINK I could roll back to if needed. – isaunders501 Mar 17 '13 at 21:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.