in reply to Mp3 Renamer

This works, but it assumes that your files at least are in a format similar to artist-title.mp3. You might consider adding (artist)_title.mp3 or other common formats.

Even more interesting would be to use MP3::Info to get information out of the ID3 tag to help determine if there's an artist/title in the filename, then rename it accordingly.

Replies are listed 'Best First'.
(jeffa) Re: Re: Mp3 Renamer
by jeffa (Bishop) on Jan 07, 2001 at 12:30 UTC
    Good idea - I wanted to point out that when checking ID3 tags using MP3::Info that you should always first make sure that there is one:
    while(<FILES>) { my $tag = get_mp3tag($file) or next; # do stuff }
    otherwise you could get some nasty results. ;)

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: Re: Mp3 Renamer
by Anonymous Monk on Jan 07, 2001 at 22:50 UTC
    Thanks for the replies. Here is an updated snippet:
    #!/usr/bin/perl print "Enter the <dir> where the mp3's are found: "; chomp ($dir = <stdin>); chdir($dir) or die "Canot change $dir :$!\n"; opendir(DIR, $dir) or die "Cannot open $dir:$!\n"; @songs = readdir(DIR); closedir(DIR); @songs=grep(/(.mp3)|(.MP3)|(.mP3)|(.Mp3)/,@songs); foreach(@songs) { @id = id3check($_); if($id[0] && $id[1]) { rename $_,"$id[0] - $id[1].mp3"; print "$id[0] - $id[1].mp3\n"; } elsif($_ =~ m/(\s*[^-]*)-\s*([^.]*).mp3/i) { my @id; $id[0] = $1; $id[1] = $2; s/\s*$|{|}//g for ($id[0], $id[1]); rename $_,"$id[0] - $id[1].mp3"; print "$id[0] - $id[1]\n"; } else { print "Could not rename $_\n"; } } sub id3check { if (open(FILE, '+<' . $_[0])) { my @id; binmode FILE; seek FILE,-128,2; read FILE,$tag,3; if($tag eq "TAG") { read FILE,$id[0],30; read FILE,$id[1],30; s/\s*$|{|}//g for ($id[0],$id[1]); return @id; } } }
              @songs=grep(/(.mp3)|(.MP3)|(.mP3)|(.Mp3)/,@songs); That's a good spot to use the /i regex modifier, which specifies case-insensitive matching:     @songs = grep /\.mp3$/i, @songs Escaping the period and adding the $ anchor are important to make sure you don't match files like 'abcmp3' or 'foo.mp3.txt'.