use strict; use MP3::Info; use File::Find; use File::Copy; use File::Basename; use File::Path; use File::Spec::Functions qw(catdir); my $genre = 'Country'; my $srcdir = '/MP3 Test/'; my $dstdir = 'c:/'; find(\&wanted, $srcdir); # note: now vars ($dir, etc.) scoped to subroutine (per Jaap) sub wanted { # anchor regex to end of filename return unless /\.mp3\z/; my $tag = get_mp3tag($_) or print "Could not access tag on $_\n" and return; return unless $tag->{"GENRE"} eq $genre; my $path = $File::Find::name; my $dir = dirname($path); my $newdir= catdir( $dstdir => $dir ); if (! -d $newdir ) { eval { mkpath($newdir, 0, 0755) }; if ($@) { print "can’t mkdir $newdir: $@"; } } # don't need basename($path), just pass copy() # a directory as the second argument copy($path => $newdir) or die "copy failed: $!\n"; }