[oak perl] regex help needed

Scott Gust scott.gust at gmail.com
Sun Apr 24 21:27:18 PDT 2005


This regex should work for you:

foreach my $file2 (glob "*mp3") {
my $newfile=$file2;
$newfile=~s/(^\d\d) (.*)(.mp3)/$2 $1$3/i;
print "Here is the new file string: \"$newfile\".\n";

# renaming stuff goes here
}

\d is for digits instead of . any character, so
"the song name.mp3" would not become
"e song name th.mp3" if  you had a track
w/o any track numbers

i on the end for case insensitive match so
.mp3 and .MP3 will both match

basically we are breaking the filename down to
component parts $1 $2 and $3 and putting it back
together how you want it with the space moved to
the right place.

hope this helps,

-scott


On 4/24/05, Sandy Santra <santranyc at yahoo.com> wrote:
> 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
> 
> _______________________________________________
> Oakland mailing list
> Oakland at pm.org
> http://mail.pm.org/mailman/listinfo/oakland
> 
>


More information about the Oakland mailing list