in reply to Recursive renaming script

After a closer look, I notice you have ($newbase = $base) on both conditions of your tertiary. My first thought was instead, my $newbase = $base;.

But, you only need $newbase if $base matches the pattern. Further, you stop using $base; you may as well make changes to it rather than $newbase.

So, perhaps this:

... for ( @found ) { my ($base,$dir) = (basename($_),dirname($_)); next unless $base =~ /$pat/; defined $opts{n} ? $base =~ s/$pat/$rep/ : $base =~ s/$pat/$rep/g; my $newname = File::Spec->catfile($dir,$base); if (-e $newname) { warn("can't rename $_ to $newname: $newname already exists\n"); } else { rename($_,$newname) or warn "can't rename $_ to $newname:$!\n"; } }

Replies are listed 'Best First'.
Re^2: Recursive renaming script
by amr noman (Initiate) on Dec 04, 2011 at 15:50 UTC

    Actually that's the way I wanted to do it initially, but then I thought that matching the same pattern twice seemed like a bad idea.
    Now I notice that the second pattern only gets processed if the file is to be renamed which is not so often, thanks.