#!/usr/bin/perl use strict; use warnings; ##################################################################### # # # script which scans recursively all folders based on # # the current directory and renames audio file-names # # to a burn-friendly format # # # ##################################################################### # constants &nb sp; # use constant MAX_FILENAME_LENGTH => 31; # main script # print "start renaming files....\n"; scanDir(); print "finished renaming files....\n"; # functions &nb sp; # sub scanDir { # open current directory opendir(CURDIR, ".") or die "could not open directory! \n"; # iterate over directory entries while(my $dirEntry = ) { # skip "." and ".." if($dirEntry eq "." || $dirEntry eq "..") { next; } # if we encounter a audio-file if(-f $dirEntry && $dirEntry =~ m/(.*mp3$)|(.*ogg$)/) { renFile($dirEntry); } # if we encounter a directory, descend into it and start recursion elsif(-d $dirEntry) { chdir($dirEntry) or die "could not open directory $dirEntry! \n"; scanDir(); } } return 0; } sub renFile { my $newFileName = my $oldFileName = shift; # delete all special chars $newFileName =~ s/[^a-zA-Z0-9_\,-]//; # shorten file name if necessary and rebuild filename $newFileName = substr($newFileName, 0, MAX_FILENAME_LENGTH) . substr($oldFileName, -4) if(length($newFileName) > MAX_FILENAME_LENGTH); rename ($oldFileName, $newFileName) or die "could not rename file $oldFileName !\n"; return 0; }