I wanted to be able to tell some tool what regexes to use in munging filenames into new filenames. I'm sure there are tools like this out there, but, nevertheless, I wrote yet another version of it. Not wanting to eval STR a bazillion times when I didn't have to, I prematurely optimised that with a single eval STR that creates a code ref instead (which is really the cool part of this CUFP to me).

#!/usr/bin/perl use strict; use warnings; my $f = shift; if ($f =~ m[^s/]) { $f = eval "sub { $f }"; } else { my $re = qr/$f/; my $to = shift; $f = eval "sub { s/\$re/$to/ }"; } for (@ARGV) { my $o = $_; $f->(); if ($o ne $_) { print "rename $o, $_\n"; rename $o, $_; } }
I save this as "myrn" and then I can just run "myrn '(\d\d)-(\d\d)-(\d{4})' '$3-$2-$1' [0-9]*.doc" to fix the YMD naming of your doc files.

Update: I just knew this was another rename tool... thanks, repellent ;-) (though this is quite specialised and easier to use, abuse, and misuse...)

Replies are listed 'Best First'.
Re: Yet another perl-rename tool
by bruno (Friar) on Feb 25, 2009 at 09:54 UTC
    Looks good! I can see myself using this.

    It's not for the faint hearted though; one should be super confident about their regex skills before applying this to a batch of critical files. Maybe with a -d switch for "dry mode" (ie, just printing to STDOUT what it would do), I'd be more comfortable using it.

    ++ nonetheless, I learned about coderefs not long ago, and I find them awesome.

Re: Yet another perl-rename tool
by chb (Deacon) on Feb 25, 2009 at 11:31 UTC
    This can be pretty dangerous, there is no check wether the target filename already exists. For example, imagine you want to convert filenames to uppercase, but there is already an uppercase filename you unfortunately overlooked:
    ls -1
    
    a
    b
    c
    A
    
    The script would rename 'a' to 'A' and the former contents of 'A' would be lost.

    For more fun, it is even possible to accidentally rename a file early in the list into a filename that is present later in the list, e.g. if you want to add 9 to numbers present in photo filenames:

    ls -1
    photo-1
    photo-2
    photo-3
    photo-4
    photo-5
    photo-6
    photo-7
    photo-8
    photo-9
    photo-10
    
    should become
    photo-10
    photo-11
    photo-12
    photo-13
    photo-14
    photo-15
    photo-16
    photo-17
    photo-18
    photo-19
    
    The regexp would look somewhat like s/(\d+)/sprintf('%d', $1 + 9)/e (untested...) The loop first turns photo-1 to photo-10 (clobbering the original photo-10), later the clobbered photo-10 will become photo-19. The old photo-10 is gone, a new photo-10 is nowhere to be found and the new photo-19 is equal to the old photo-1. Confusion!

    Maybe it is better to build a list of filename replacements first and check this list for these issues before doing any permanent change.

      It's quite easy to avoid the problem:
      #!/usr/bin/perl use strict; use warnings; my $f = shift; if ($f =~ m[^s/]) { $f = eval "sub { $f }"; } else { my $re = qr/$f/; my $to = shift; $f = eval "sub { s/\$re/$to/ }"; } for (@ARGV) { my $o = $_; $f->(); if ($o ne $_) { if ( ! -e $o ) { print "rename $o, $_\n"; rename $o, $_; } else { print "can't rename : $o exists\n" } } }
        Yes, checking wether the target file exists will prevent lost files. I still think building and checking a replacement list is better, because it allows you to skip the whole batch of operations instead of having to dig through the output to fix rejected rename operations.
Re: Yet another perl-rename tool
by repellent (Priest) on Feb 25, 2009 at 09:36 UTC