Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

rename directories

by ashok (Sexton)
on Dec 14, 2000 at 03:09 UTC ( [id://46505]=perlquestion: print w/replies, xml ) Need Help??

ashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to write a perl script on unix like this. . First I have to check all the directories down the tree if any are with blank in between.
for ex: /mydir/program files/dir1/dir2/last dir should be renamed to /mydir/program-files/dir1/dir2/last-dir
Can anyone help me? Thanks a lot. Ashok

Replies are listed 'Best First'.
Re: rename directories
by fundflow (Chaplain) on Dec 14, 2000 at 03:13 UTC
    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

      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")
      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.
YAV (Yet Another Version)
by dmitri (Priest) on Dec 14, 2000 at 10:05 UTC
    Here's another version... Seemed like a fun problem (I love recursion), so I hacked this up:
    #!/usr/local/bin/perl -w use strict; sub dirs { my @retval; opendir DIR, "."; my @file = readdir DIR; closedir DIR; for (@file) { next if /^\.\.?$/ or -l $_; push @retval, $_ if -d $_; } return @retval; } sub rename_dirs { my $dir = shift; chdir $dir; my $newname; for (dirs()) { rename_dirs($_); if (/ /) { ($newname = $_) =~ s/ /-/g; rename $_, $newname; } } chdir ".."; } rename_dirs($ARGV[0]);

    Update: -l $_ added after receiving a comment by merlyn. thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://46505]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 04:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found