Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and Renaming files via RegExp

Status
Not open for further replies.

jrottman

Programmer
Jun 17, 2005
47
I know it is possible to search a string and determine whether or not the string has the regexp with in the string.

What I am trying to accomplish is renaming of files that have either non-standard or illegal chars with in the file name, but still retain the file extension.

IE
If I were to get this file 12(1)test.txt, I would like to rename it to 121test.txt.

With in coldfusion or java, I would using something like reFind(regExp,file) and if determined to have illegal chars rename file.

This is the standard regExp that I use with in cf/java as white list chars. A-Za-z0-9.-_.

Everything I have tried has pretty much come up with nada.

Can someone point me in the right direction.

This is my current code.

#! Scrubs illegal chars from document names
sub scrubDoc {
foreach (@_){
print "$_";
rename("$_" , "/^A-Za-z0-9.-_/");
}
}
 
Code:
    #! Scrubs illegal chars from document names
    sub scrubDoc {
        foreach (@_){
            print $_;
            my $temp = $_;
            $temp =~ tr/A-Za-z0-9.-_//cd;
            rename($_ , $temp);
        }
    }



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You should also check the rename() function for failure:

Code:
rename($_ , $temp) or die "Couldn't rename <$_> <$temp>: $!";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top