in reply to Re: Yet another perl-rename tool
in thread Yet another perl-rename tool

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" } } }

Replies are listed 'Best First'.
Re^3: Yet another perl-rename tool
by chb (Deacon) on Feb 25, 2009 at 14:58 UTC
    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".