i personally like my mp3 files named a particular way. it drives me nuts if they aren't named this way. i've also got them wonderfully organized in my filesystem, but that's another story.
what i have here is a script that i drag a folder full of mp3's on to (it's on my desktop right now), and it will rename all of the files to my liking.
the format is: ## - <Artist> - <Song Name>.mp3 (where ## is a two digit track number).
so here it is... i haven't modified it in a bit, so it could probably be optomized, improved... but it's currently working, and i have other things to do, so it'll stay as is for now.
#!/usr/bin/perl use strict; use MP3::Info; my $folder = $ARGV[0]; if ( $folder eq "" ) { print "You suck\n"; # or any other kind error message! exit; } opendir( DIR, $folder ); my @files = map { $folder . "\\" . $_ } readdir( DIR ); closedir( DIR ); open FILE, ">", "debug.txt"; foreach ( @files ) { my $mp3 = new MP3::Info( $_ ) or next; my $new_name = ( $mp3->tracknum ne "" ? sprintf( "%02d", $mp3->tracknum ) . " + - " : "" ) . $mp3->artist . " - " . $mp3->title . ".mp3"; $new_name =~ s/[\/:?]//g; # take out illegal characters $new_name = $folder . "\\" . $new_name; print $new_name, "\n"; print FILE "$_ -> $new_name\n"; rename $_, $new_name; } close FILE;

Replies are listed 'Best First'.
Re: mp3 renamer
by ghenry (Vicar) on May 17, 2005 at 10:03 UTC

    print "You suck\n";

    That's a very informative error message ;-)

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
      yeah... i wrote this for myself, and i never actually call the script explicitely, only by dragging a folder onto it - so i should never see that message anyway. would it be production code, that would be quite a bit... nicer. but i like to do stuff like that sometimes, it makes coding even more fun for me. at my last job, i also liked to leave little notes to future maintainers in my comments... i hope somebody reads them someday.
Re: mp3 renamer
by BerniBoy (Acolyte) on May 17, 2005 at 19:28 UTC
    Hmm ...
    You seem to rely on the ID3 info. But what if the filenames are okay, and the tags contain trash? (seen that a lot!)
      ah... good point. that could probably be improved!
      in my case (man, now i feel like i'm defending my code, instead of just making it better), i know that the tags are always right, because i'm particular about that too, and i'm particular about that first, usually so my script does work ;).
        Lucky you! ^^

        Well, once upon a time i fell for that one, too. So i added some little bit of user interaction. If you are interested i could search my harddisks for the code. ;)