in reply to rename directories

Probably something like the following. Before running it, follow the link of (3) and run the script there.
#!/usr/bin/perl -w use strict; use File::Find; my $dir = '.'; # or wherever finddepth(\&myrename, $dir); sub myrename { return unless -d; # if you only care about dirs return unless / /; my $oldname=$_; s/ /_/g; return if ($oldname eq $_); if (rename $oldname,$_) { print "renamed $File::Find::name to $_\n"; } else { warn "Couldn't rename $File::Find::name to $_: $!"; } }



Notes:
1. Adapted from 32038
2. PM is acting wierd today!
3. This was around before: rename directories recursively to remove spaces from names has a safer way.
4. Changed according to tye's suggestion

Replies are listed 'Best First'.
(tye)Re: rename directories
by tye (Sage) on Dec 14, 2000 at 03:33 UTC

    Don't you want to use finddepth() so that by the time you rename the directory File::Find is done with it?

            - tye (but my friends call me "Tye")
Re: Re: rename directories
by ashok (Sexton) on Dec 14, 2000 at 04:21 UTC
    Great. It works for me. A minor change I have made. Since $new_name is not declared , it is warning me. Hence I have replaced $new_name to $_ and works. Thanks a lot. One more help. I am new to perl. Can I know what it means of the code?
    return unless / /;
    Thanks again. Ashok
      It's a regex checking for a space in the name, without which you have no reason to continue the sub.