#!/usr/bin/perl use File::Find; # top-level directory input as CLI argument my $root = shift; # Change this to the directory you want artwork copied to my $artwork_path = $root . '/artwork'; mkdir $artwork_path, '0777' or die "Error: Cannot create directory. [ $! ]" unless (-d $artwork_path); find(\&process, $root); sub rename_cover { my $infile = shift; my $music_root = shift; # make a copy of filename my $file = $infile; # remove root from path $file =~ s/$music_root\/?//ig; # strip - & . from album or artist $file =~ s/[\-\.]//g; # split artist and album into two pieces $file =~ /^([^\/]+)\/(.+)\/.+$/; my $artist = $1; my $album = $2; # remove trailing in parentheses w/ contents from album $album =~ s/\(.*\)\s*$//g; # put backtogether with - $file = $artist . '-' . $album . '.jpg'; # lowercase $file = lc $file; # remove anything that isn't alpha-numeric, - or . $file =~ s/[^0-9a-z\-\.]//g; # copy the file w/ new name my $test = copy($infile, $artwork_path . '/' . $file); # return new filename or 0 if failed return (($test) ? $file : '0'); } # search for all cover.jpg beneath root, and rename them sub process { /cover\.jpg$/ig && rename_cover($File::Find::name, $root); }