in reply to File::Find: Problem with renaming folders recursively

If you think File::Find is a little obscure, perhaps you should try the CPAN module File::Find::Rule instead. This might be one way to accomplish your goal:
use warnings; use strict; use File::Find::Rule; my $path = $ARGV[0]; my $from = $ARGV[1]; my $to = $ARGV[2]; die "You must supply a full directory path" unless (-e $path && -d $pa +th); my @dirs = File::Find::Rule->directory() ->name("*$from*") ->in($path); for (@dirs) { my $orig = $_; s/$from/$to/g; rename $orig, $_ or die "Can not rename $orig to $_: $!"; }
  • Comment on Re: File::Find: Problem with renaming folders recursively (File::Find::Rule)
  • Download Code

Replies are listed 'Best First'.
Re^2: File::Find: Problem with renaming folders recursively (File::Find::Rule)
by runrig (Abbot) on Mar 20, 2009 at 17:28 UTC
    You're going to have to do a depth first search if you want to rename directories and their sub-directories, and File::Find::Rule doesn't seem to have an option for that. Though, I think you could sort the results by the number of directory separators (from greatest to least) -- yeah, that oughta work.
Re^2: File::Find: Problem with renaming folders recursively (File::Find::Rule)
by Anonymous Monk on Mar 20, 2009 at 17:26 UTC
    Thanks. This renames only the first directory in the directory tree, not the directories under the first directory. I'm lost.
        Thanks! That worked perfectly!!!!