Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How to rename to lowercase every file in a directory and its subdirectories?

by Alex the Serb (Monk)
on Dec 20, 2001 at 19:03 UTC ( #133482=note: print w/replies, xml ) Need Help??


in reply to How to rename to lowercase every file in a directory and its subdirectories?

you have to use recursion, because you dont know how much deep all subdirectories goes
#!/usr/bin/perl -w use strict; my $path_to_dir = shift; dir("$path_to_dir"); sub dir { opendir(DIR,"$_[0]"); my @list_of_files = readdir(DIR); foreach(@list_of_files) { if($_ ne "." && $_ ne "..") { if(-d "$_[0]/$_") { dir("$_[0]/$_"); } else { rename "$_[0]/$_","$_[0]/"."\L$_"; } } } }
  • Comment on Re: How to rename to lowercase every file in a directory and its subdirectories?
  • Download Code

Replies are listed 'Best First'.
Re: Answer: How to rename to lowercase every file in a directory and its subdirectories?
by hsmyers (Canon) on Dec 21, 2001 at 00:44 UTC
    Pretty much the idea, but you seem a little confused by variables. Lightly edited (changed to a print statement to protect my innocent file names), here is your code:
    #!/usr/bin/perl use strict; use warnings; dir('.'); sub dir { opendir( DIR, shift ); my @list_of_files = readdir(DIR); foreach (@list_of_files) { if (-d $_) { dir($_) if $_ !~ /^\.\.?$/; } else { print $_," would become \L$_,\n"; } } }
    Things to note:
    • If you mean to use a variable as a parameter, just do it, don't embed it in quotes
    • Your use of $_[0] isn't needed, easier way to do the same is shown
    • Post conditions and regexs are your friend, use them and they will reward you!

    –hsm

    "Never try to teach a pig to sing…it wastes your time and it annoys the pig."
Re: Answer: How to rename to lowercase every file in a directory and its subdirectories?
by Anonymous Monk on Jan 13, 2009 at 15:54 UTC
    hsmyers - I cannot reliably make your code work. While the print function appears to show what was intended, it does not properly recurse through, not will it rename the files. However, Alex's code does give the intended results.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://133482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-08-12 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?