[oak perl] regex help needed

David Fetter david at fetter.org
Sun Apr 24 22:02:41 PDT 2005


On Sun, Apr 24, 2005 at 09:38:37PM -0400, Sandy Santra 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.

OK, just generally, it's a Good Thing(TM) to keep your lines at or
under 80 characters, and your tabs either as actual tab characters or
as 4-space indents.  I could just barely read what you pasted below,
which made debugging harder than it needs to be.  Refer to perldoc
perlstyle on more of these.

#########################################################################
#                                                                       #
# 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; # Yay!!!

chdir "c:/temp"
    or die "cannot chdir to that directory: $!"; # no comment needed.

#######################################################################
#                                                                     #
# The following is more idiomatic perl, and actually says what you're #
# iterating over..                                                    #
#                                                                     #
#######################################################################
foreach my $dir (<*>) {
    next unless -d $dir; # test if it's a directory before attempting to chdir.
    chdir $dir
        or die "cannot chdir to that directory: $!";
    foreach my $file (<*>) {
        # Eliminate anything that's not a file.
        next unless -f $file
        #############################################
        #                                           #
        # Find things that match in sets of parens: #
        # First is the track number                 #
        # Second is the name                        #
        # Third, the suffix.                        #
        #                                           #
        #############################################
        next unless $file =~ /^(\d{2})\s+(.*)(\.mp3)$/ix;
        my $newfile = "$2 $1$3";
        if (-e $newfile) {
            warn "can't rename $file2 to $newfile: $newfile already exists\n";
        } else {
            # Indent here.  It's a whole different thing.
            if (rename $file, $newfile) {
                print "renamed $file to $newfile\n";
            } else {
                warn "rename $file to $newfile failed: $!\n";
            }
        }
    }
    print "Directory $dir completed.\n";
    chdir '..'
        or die "cannot chdir to that directory: $!";
}

HTH :)

Cheers,
D
-- 
David Fetter david at fetter.org http://fetter.org/
phone: +1 510 893 6100   mobile: +1 415 235 3778

Remember to vote!


More information about the Oakland mailing list