Script to Rename MP3's

I recently got a new car stereo due to the other one being stolen. I am almost entirely happy with the model that I ended up purchasing, but one thing that it does, which is really obnoxious, is that it doesn’t sort the files correctly unless the track number is early on in the file name. Even if all tracks are “FooBarBaz 01 - name.mp3” it seems to ignore the number unless it’s the very beginning of the file name. Anyway, easy fix:

use strict;
use warnings;
use feature ':5.10';

use Music::Tag;
use File::Find::Rule;
use File::Basename "fileparse";
use File::Copy "move";
use File::Spec;
my $directory = shift || '.';

my @songs
   = File::Find::Rule->file()->name( '*.mp3' )
   ->in( $directory );

foreach my $song (@songs) {
   my $info = Music::Tag->new($song);
   my $track = $info->track;
   my $title = $info->title;
   my (undef, $dir, $suffix) =
      fileparse($song, qr/\.[^.]*/);
   if ($track and $title) {
      my $newfilename = File::Spec->catfile(
         sprintf "%02d %s%s", $track, $title, $suffix
      if ($song ne $newfilename) {
         say "renaming $song to $newfilename";
         move $song, $newfilename;

It doesn’t really deal with illegal characters, it just doesn’t rename those files. Eventually I’ll get around to doing that. Anyway, just figured someone might be interested/want to copy paste this.

Posted Sun, May 31, 2009