Note: this was written for a <duck> windows computer, and is not directly portable without a few edits... I run it as a service under win2k. You can also run it from command-line with -d, it will just process once and quit.
Note 2: For them what don't know, eMusic.com is a great DRM-free mp3 service. Their jazz collection is huge. They just don't offer much customizability in the naming of files, and I like mine [artist - album] 01 songname.mp3" hence....
#!/usr/bin/perl
# emusicfix.pl
#
# Will watch a specified folder for new MP3 files downloaded from emus
+ic.com
# and fix the filenames, then place them in their own directories by a
+rtist.
##################################################
# Dir where you want the artist directories created:
my $basedir = "d:\\music\\";
# Dir where you will place newly-downloaded files:
my $watch = "d:\\music\\em_save\\";
# Delimiter used by the filenames to separate artist, track etc.:
my $delimiter = '-';
# Field orders:
# The default is for names like:
# Artist_Name-Album_Name-Track_Name-Song_Name.mp3
my %f = (
artist => 0,
album => 1,
track => 2,
song => 3,
);
# That's it!
my $count = 0;
my $debug = 0;
if ($ARGV[0] =~ /-d/) {
$debug = 1;
processdir();
} else {
daemonize();
}
# You may want to consider running this as a service under win2k et al
+.
sub daemonize {
while (1) { #run forever
processdir();
sleep 20;
}
}
sub processdir {
my (@list);
$count = 0;
for (@list = sort `dir /b $watch\\*.mp3`) {
chomp;
fixfile($_) unless ($_ =~ /.*File Not Found.*/i);
}
if ($debug) {
if ($count) { print "Moved $count files\n"}
}
}
sub fixfile {
my $filename = shift;
chomp $filename;
die "Bad infile" unless ($filename =~ /\.mp3$/);
my (@file) = split/-/,$filename;
for (@file) {$_ =~ s/_/ /g;}
my $newfilename = '['.$file[$f{artist}].' - '.$file[$f{album}].']
+'.$file[$f{track}].' '.$file[$f{song}].'';
$newfilename =~ s/ s /\'s /gi;
$newfilename =~ s/ m /\'m /gi;
my $source = $watch.$filename;
my $dest = $basedir.$file[$f{artist}].'\\'.$newfilename;
if (! makedir($basedir.$file[$f{artist}])) {
print "Skipping file, can't make dir for $file[$f{artist}]" if
+ $debug;
next;
}
if (! -e $dest) {
my $result = `copy "$source" "$dest"`;
if ($result =~ /1 file\(s\) copied\./) {
$count++;
print "$dest [OK]\n" if $debug;
$del = `del "$watch$filename"`;
print "Error deleting: $del\n" if ($del && $debug);
}else{
print "Can't copy\n $source to\n $dest\n" if $debug;
}
} else {
print "Skipping existing $filename\n" if $debug;
}
}
sub makedir {
my $dir = shift;
if (-e $dir) {
return 1;
}else{
return mkdir $dir;
}
}