Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

786 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