use MP3::Tag; use File::Find; use File::Copy; use strict; my @fileList; my $debug = 0; my $dir; if ($0=~m#^(.*)\\#) { $dir = "$1"; } elsif ($0=~m#^(.*)/# ) { $dir = "$1"; } else { `pwd` =~ /(.*)/; $dir = "$1"; } $dir =~ s/\\/\//g; my $baseDir = "X:/Deleted"; my $iTunesDir = "X:/Share/Music"; find(\&matchMP3, $dir); print "Files found: ".($#fileList+1)."\n"; my $count = $#fileList+1; foreach my $item ( @fileList ) { print "Reading ID3: ".fitToScreen($item)."\n"; my $mp3 = MP3::Tag->new($item); $mp3->get_tags(); print "No ID3v2 tag found.\n", if not exists $mp3->{ID3v2}; print "No ID3v1 tag found.\n", if not exists $mp3->{ID3v1}; next, if (not exists $mp3->{ID3v2} and not exists $mp3->{ID3v1}); print "ID3v2 tag found...\n", if exists $mp3->{ID3v2}; print "ID3v1 tag found...\n", if exists $mp3->{ID3v1}; my $tag = (exists $mp3->{ID3v2})? $mp3->{ID3v2} : $mp3->{ID3v1}; my $artist = $tag->artist(); my $album = $tag->album(); my $disc = $tag->get_frame("TPOS"), if exists $mp3->{ID3v2}; my $track = $tag->track(); my $title = $tag->title(); if ($artist =~ /\x00/ || $album =~ /\x00/ || $title =~ /\x00/ ) { print "Found multiple values!\n"; print "title:\t\t$title\n"; print "title (fixed):\t".&fixName($title)."\n"; print "artist:\t\t$artist\n"; print "artist (fixed):\t".&fixName($artist)."\n"; print "album:\t\t$album\n"; print "album (fixed):\t".&fixName($album)."\n"; my $pause = <>, if $debug; } if ( not $title ) { my @parts = split("/",$item); $title = @parts[$#parts]; print "No IDv3 title found. Using filename $title.\n"; } else { $title = &fixName($title)."\.mp3"; } print " Title_____".&fixName($title)."\n", if $debug; print " Artist____".&fixName($artist)."\n", if $debug; print " Album_____".&fixName($album)."\n", if $debug; print " Track_____".$track."\n", if $debug; print " Disc______".$disc."\n", if $debug; my $albumDir = $baseDir."/".&fixName($artist)."/".&fixName($album); my $checkDir = $iTunesDir."/".&fixName($artist)."/".&fixName($album); my $fileName; $fileName .= sprintf("%1d", $disc)."-", if $disc; $fileName .= sprintf("%02d", $track)." ", if $track; $fileName .= $title; $mp3->close(); if ( -f $checkDir."/".$fileName ) { print "Found: $checkDir/$fileName\n"; print "File is already part of the iTunes library. Skipping...\n"; $count--; print "Done. (".($#fileList+1-$count)." of ".($#fileList+1).")\n\n"; my $pause = <>, if $debug == 2; next; } print "Checking if $albumDir exists..."; if ( -d $albumDir ) { print "it does.\n"; } else { print "not found. Creating..."; if ( not createDir($albumDir) ) { print "failed. $!\n"; print "Exiting application. Please check your directory permissions and retry.\n"; my $pause = <>; exit 1; } print "success!\n"; } if ( -f $albumDir."/".$fileName ) { my $count = 1; while ( -f $albumDir."/".$count." ".$fileName ) { $count++; } $fileName = $count." ".$fileName; } print "Copying file $fileName...\n"; if ( not copy($item, "$albumDir/$fileName") ) { print "Error copying $albumDir/$fileName: $!\n"; print "Exiting application. Please check your directory permissions and retry.\n"; my $pause = <>; exit 1; } my $pause = <>, if $debug == 2; $count--; print "Done. (".($#fileList+1-$count)." of ".($#fileList+1).")\n\n"; } print "\nPress return to continue..."; my $pause = <>; exit; sub matchMP3() { print "Searching directory $File::Find::name...\n", if -d $File::Find::name; if ( -f $File::Find::name ) { if ( $File::Find::name =~ /\.mp3/ ) { #print "Adding file $File::Find::name\n", if $debug == 2; push (@fileList, $File::Find::name); } } } sub fitToScreen() { my $filespec = shift; return $filespec, if length $filespec < 67; my @parts = split("/", $filespec); my $name = @parts[$#parts]; my $len = length $name; my $pre = 64 - $len; return substr($filespec, 0, $pre)."...".$name; } sub fixName() { my $name = shift; ($name, my @trash) = split /\x00/, $name , if $name =~ /\x00/; $name =~ s/[\\\/:\;\"\<\>\*\|\?\[\]]/_/g; $name =~ s/\s{2,}/ _ /g; $name =~ s/\.{3,}/_/g; $name = substr($name, 0, 40), if (length($name) > 40); $name = trim($name); return $name; } sub createDir() { my $dir = shift; my @parts = split("/",$dir); my $newDir = @parts[0]."/".@parts[1]; if ( not -d $newDir ) { if ( not mkdir($newDir) ) { return -1; } } for ( my $i=2; $i <= $#parts; $i++ ) { $newDir .= "/".@parts[$i]; if ( not -d $newDir ) { if ( not mkdir($newDir) ) { return -1; } } } return 1; } sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }