Solved

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

Posted on 2006-10-31
4
606 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
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
Comment Utility
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
Comment Utility
#!/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
Comment Utility
Sorry, I could have sworn that I closed this one. ozo, thank you for you effort. That worked perfectly.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
This video discusses moving either the default database or any database to a new volume.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now