[oak perl] regex help needed

Sandy Santra santranyc at yahoo.com
Sun Apr 24 18:38:37 PDT 2005


Hi, folks.  I just spent 3 hours debugging something; and now that I'm 
done, I'm thinking there has to be a better way to do this.

I just wanted a script that would strip the first few characters off each 
file name in all subdirectories of a directory and then stick said stripped 
string back into the middle of the file name.  I'm just a newbie at regex, 
so apologies for the 4 lines of regex's (plus everything else) to 
accomplish this; and a few lines were written just to deal with getting a 
single space into the right place...maybe there's a trick for that.  If 
anyone has an idea on how to do it faster or easier, or any other comments 
about the rest of the code, I'd love to hear them.  Thanks.


#########################################################################################
# THIS PERL SCRIPT DOES THE FOLLOWING:
# Strips two-digit track # from beginning of each file name and puts track 
number
# at the end of that file name (but before the file extension).
# EXAMPLE:
# A file that looks like this: "03 This Song Rocks.MP3" should come out
# looking like this "This Song Rocks 03.MP3" after the script has completed.
#########################################################################################
use strict;

chdir "c:/temp" or die "cannot chdir to that directory: 
$!";                    #target correct dir
foreach my $file (glob "*") 
{                                                           #begin loop for 
all subdirs
         chdir "$file" or die "cannot chdir to that directory: 
$!";                      #change dir to subdir
         foreach my $file2 (glob "*") 
{                                                  #begin loop for all files
                 my $newfile = 
$file2;                                                   #copy filename to 
dummy var
                 $newfile =~ 
s/.mp3//;                                                   #strip ".MP3"
                 $newfile =~ 
s/(^..)//;                                                  #strip and copy 
track #
                 $newfile = "$'" . " 
$1.MP3";                                    #concat filename + track # + .MP3
                 $newfile =~ s/^ 
//;                                                     #strip leftover 
space at beginning
                 print "Here is the newfile string: 
\"$newfile\".\n";                    #print new dummy var

                 if (-e $newfile) {
                         ## warn "can't rename $file2 to $newfile: $newfile 
exists\n";
                 } elsif (rename $file2, $newfile) 
{                                     #copy dummy var to filename
                         ## success, do nothing
                 } else {
                         warn "rename $file2 to $newfile failed: $!\n";
                 }
         }
         print "Directory $file completed.\n";
         chdir ".." or die "cannot chdir to that directory: 
$!";                 #change dir back to parent
                                                                                                 #for 
next iteration of loop
}





--Sandy Santra



More information about the Oakland mailing list