#!/usr/bin/perl -w use strict; use Cwd; use File::Spec::Functions qw(catdir catfile); use File::Copy; use MP3::Info; die "usage: $0 dirname [dirname ...]\n" unless @ARGV; my $cwd = cwd(); my %skip; for(@ARGV) { chdir($_) or (warn("Couldn't chdir to $_\n"), next); opendir my($dh), "."; for my $file (grep /\.mp3\z/i and -f, readdir $dh) { my ($artist, $title) = @{ get_mp3tag($file) }{qw(ARTIST TITLE)}; next unless $artist and $song; my $dirname = lc $artist; s/\bthe\s*//g, s/^\s+//g, s/\s+$//g, tr/ /_/ for $dirname; my $targ_dir = catdir("..", $dirname); unless($skip{$directory} or -e $targ_dir) { print "Create $dirname folder (y/n)? "; =~ /^y/i ? mkdir $targ_dir : undef $skip{$directory}; } if(-d $targ_dir) { my $target = catfile($targ_dir, "$artist - $title.mp3"); print "$file -> $target\n"; move($file, $target); } else { warn( "Skipping $file, $directory ", (-e $targ_dir ? "is not a directory" : "was not created" ), ".\n" ); } } chdir($cwd); }