Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A Perl script to find duplicate iTunes tracks' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A Perl script to find duplicate iTunes tracks
Authored by: snakesalive on Jul 02, '10 07:09:03AM
Here's a script I knocked together to remove a few hundred duplicates of the form musicfile.mp3, musicfile 1.mp3. The script locates such pairs of files and checks that they are the same size. It prompts for confirmation before deleting the duplicates. The working directory should be the iTunes Music Library, e.g. ~/Music/iTunes/iTunes Media/Music/

#!/usr/bin/perl

use strict;
use warnings;

use File::Find;
use File::Basename;

my $act = 0;
my $num = 1;

sub process
{
        / $num\.mp3$/ or return;
        my $file1 = $File::Find::name;
        my $file2 = $_;
        $file2 =~ s/ $num\.mp3$/.mp3/;
        if (-e "$file2") {
                my $size1 = -s "$_";
                my $size2 = -s "$file2";
                if ($size1 == $size2)
                {
                        if ($act != 0)
                        {
                                print "Deleting: $_\n";
                                unlink("$_");
                        }
                        else
                        {
                                print "\"$_\" (duplicate: \"$file2\", same size)\n";
                        }
                }
        }
}

find(\&process, '.');
print "Confirm delete? [y/n] ";
my $ans = <>;
chomp($ans);
if ("$ans" eq "y")
{
        $act = 1;
        find(\&process, '.');
}
else
{
        print "Process cancelled\n";
}


[ Reply to This | # ]
A Perl script to find duplicate iTunes tracks
Authored by: afingal on Jul 02, '10 12:54:36PM

snakesalive,

Do I presume correctly that your script depends on the very low probability that two different songs will have the exact same file size (to the byte?)



[ Reply to This | # ]
A Perl script to find duplicate iTunes tracks
Authored by: snakesalive on Jul 02, '10 03:26:28PM

To be more precise the two files should be detected as exact duplicates by this script if they satisfy all of the following:
a) are in the same folder
b) have filenames of the form "name.mp3" and "name 1.mp3"
c) have the same size.
This should pick up the situation when tagged mp3s are imported twice.



[ Reply to This | # ]