Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Merge files & delete row based on criteria using Perl. 1 104
crawling gofundme 4 132
How to search multiple patterms in a file with perl? 4 82
perl getopt long help 34 71
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

828 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