iTunesCleaner
A perlscript I hacked up to get rid of files that have been deleted in the iTunes Music Library, but not deleted from the HD. It seems to need some case insensitivity added or else you risk deleting files you don’t want to, but for the most part it works…
#!/usr/bin/perl -w
if (@ARGV == 0) {
print "must specify a command to run, e.g. rm\n";
exit(0);
}
$homeDir = "/Users/aidan";
$musicDir = "/Volumes/Holding Bay/Music"
$iTunesDB = $homeDir . "/Music/iTunes/iTunes Music Library.xml";
$iTunesOut = $homeDir . "/Desktop/iTunesParsed.txt";
$sortediTunesOut = $homeDir . "/Desktop/s_iTunesParsed.txt";
$otherFile = $homeDir . "/Desktop/otherFile.txt";
$sortedOtherFile = $homeDir . "/Desktop/s_otherFile.txt";
$sorted = $homeDir . "/Desktop/sorted.txt";
$final = $homeDir . "/Desktop/extraMusic.txt";
open(DB, $iTunesDB) || die "Cannot open $iTunesDB";
open(OUT, ">$iTunesOut") || die "Cannot open $iTunesOut";
@lines = <db>;
foreach $_ (@lines) {
if (/^\t\t\t<key \>Location< \/key><string>file:\/\/localhost(.*)< \/string\>$/) {
$line = $1;
$line =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
$line =~ s/#38;//g;
print OUT "$line\n";
}
}
close(OUT);
close(DB);
system "find \"$musicDir\" -regex \".*[mp3|m4a]\" -type f> $otherFile";
system "sort $iTunesOut > $sortediTunesOut";
system "sort $otherFile > $sortedOtherFile";
system "sort -m -o $sorted $sortediTunesOut $sortedOtherFile";
open (FINAL, ">$final") || die "Could not open $final";
open (SORTED, $sorted) || die "Could not open $sorted";
@lines = <sorted>;
for ($i = 0; $i < $#lines; $i++) {
if ($lines[$i] eq $lines[$i+1]) {
$i = $i + 1;
} else {
print FINAL $lines[$i];
}
}
close (SORTED);
close (FINAL);
open (FINAL, $final);
@lines = <FINAL>;
foreach $file (@lines) {
$file =~ s/\n//g;
foreach $cmd (@ARGV) {
system("$cmd \"$file\"");
print ("$cmd \"$file\"");
}
}
close (FINAL);
`rm $otherFile $iTunesOut $sorted $sortediTunesOut $sortedOtherFile $final`;