Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to recursively replace uderscores with spaces and lowercase file/folder names

Posted on 2006-10-31
4
Medium Priority
?
652 Views
Last Modified: 2011-09-20
Hello,

I need a script to recursively replace spaces with underscores in file/folder names and also lowercase all file/folder names. I am new to Perl so I would greatly appreciate a bit of detail in how the solution works also. Please let me know if any additional information is required.
0
Comment
Question by:austerhaus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 1200 total points
ID: 17845432
perl -MFile::Find -e 'find({wanted=>sub{my $n=$_; rename $_,$n or warn "$n $!" if $n=~tr/ A-Z/_a-z/},bydepth=>1},".")'
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17849100
#!/usr/bin/perl -w
use strict;
use File::Find;

find(\&rename, ".");                   # search the current directory and all under it

sub rename {
    if (/[\sA-Z]/) {                       # if there are spaces or capital letters
        my $newname = lc($_);     # lowercase the filename
        $newname =~ s/\s+/_/g;   # replace all spaces with underscores
        rename ($_, $newname)     # rename the file or issue a warning
        or warn "Couldn't rename $File::Find::name to $newname: $!\n";
    }
    return;
}
0
 
LVL 1

Author Comment

by:austerhaus
ID: 18093997
Sorry, I could have sworn that I closed this one. ozo, thank you for you effort. That worked perfectly.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question