in reply to Yet another perl-rename tool

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.

Replies are listed 'Best First'.
Re^2: Yet another perl-rename tool
by wazoox (Prior) on Feb 25, 2009 at 14:35 UTC
    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.
        Personally I prefer to have the processing be done on a list while skipping exceptions, unless requested otherwise. That way I could take care of remaining *few* offending ones on case by case basis. Ideally, operation would be done quietly (unless asked to be verbose) and errors would go to standard error, which can be redirected to a file to avoid "dig[ging] through the output to fix rejected rename operations".