#!/bin/perl -w use strict; use Cwd; my @ignore = (".", "..", "discid"); sub quote ($) { my $a = $_[0]; $a =~ s/!/"'!'"/g; return "\"$a\""; } opendir DIR, "." or die "can't list files\n"; my @files = sort(readdir(DIR)); closedir DIR; my %ignore = (); foreach my $i (@ignore) { $ignore{$i} = 1; } my $albumartist = undef; cwd() =~ /\/([^\/]*)$/ or die "can't get cwd\n"; my $dirname = $1; if ($dirname =~ /^(.*?) - /) { $albumartist = $1; } foreach my $file (@files) { next if defined($ignore{$file}); my $orig = $file; $file =~ tr/[]/()/; $file =~ s/[ \(][a-z]/uc $&/eg; unless ($file =~ /^\(?(\d+)(?:\.| -)?\)?\s*(.*)\.([^\.]+)$/) { die "unrecognised name $file\n"; } my ($num, $name, $ext) = ($1, $2, $3); $name =~ s/^(Various|Various Artists) -\s*//; my ($artist, $title) = (undef, undef); if ($name =~ /^(.*?) - (.*)/) { ($artist, $title) = ($1, $2); } else { $title = $name; } unless (defined($artist)) { unless (defined($albumartist)) { die "can't guess artist for $file; specify\n"; } $artist = $albumartist; } my $new = sprintf("%02d %s - %s\.%s", int($num), $title, $artist, $ext); print "mv " . quote($orig) . " " . quote($new) . "\n"; }