Solved

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

Posted on 2006-10-31
4
636 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 300 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

738 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