CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Renaming files on local system

 



wjbell
stranger

Nov 14, 2001, 10:25 AM

Post #1 of 5 (1019 views)
Renaming files on local system Can't Post

I want to write a script that takes each file in a directory and change it from uppercase to lowercase. Say the file is named:

ARIAL.TTF

The script would rename it to arial.ttf

I want to be able to do this with all files in a directory. I'm not sure how to do this. Any ideas? Thanks.



thodi
stranger

Nov 15, 2001, 12:10 AM

Post #2 of 5 (1015 views)
Re: Renaming files on local system [In reply to] Can't Post

Check out Perls rename and glob functions. A simple recipe for converting the filenames would be

Code
$name =~ tr/A-Z/a-z/;

.



yapp
User

Nov 15, 2001, 4:37 AM

Post #3 of 5 (1013 views)
Re: Renaming files on local system [In reply to] Can't Post

I would not recommend globbing. To glob, the perl interpreter needs to load an other interpreter (C off course). The <*> glob command is limited, due some max. array/string size.


I type this in directly, so please check it for errors yourself.

Code
my $dir = '/the/start/dir' # or a . (single dot) 

goThoughDir( $dir );

sub goThoughDir
{ my($dir) = @_; # Copy parameters

opendir( DIR, $dir) or die "Can't open the directory: $!"; # edit Added a better error message

while( my $filename =readdir DIR ) # while there are files to read...
{
my $fullname = "$dir/$filename";
if( -f $fullname )
{
# This is a file...
rename $fullname => $dir . lc($filename); # I like to use the => in stead of a comma
}
elsif( -d $fullname )
{
# this is a directory, we can start recursion here... unless the file is a . or ..
if( $filename ne '.' && $filename ne '..')
{
goThroughDir( $fullname ); # IF this is not what you want, remove it.
# Beware for any recursion, if a directory is actually a 'shortcut', or 'reference' to an other location
}
}
}
closedir( DIR );
}

The opendir, readdir, and closedir can be used to read through directories. Beware that a . or .. causes recursion, since that sends us back to the directory we came from. You can remove the recursion feature from this code off course.

Hope it helps.

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com

(This post was edited by yapp on Nov 15, 2001, 3:38 AM)


wjbell
stranger

Nov 15, 2001, 2:32 PM

Post #4 of 5 (1010 views)
Re: Renaming files on local system [In reply to] Can't Post

Thanks. I found somthing though.

for (<*>) {
rename $_, "\L$_";
}



yapp
User

Nov 16, 2001, 3:29 AM

Post #5 of 5 (1004 views)
Re: Renaming files on local system [In reply to] Can't Post

Yes, That works.

However. the <*> glob is very inefficient. And is has limitations, as I explained in my previous post. You can use it if you like, but it is not recommended.


Code
opendir DH, '.' or die "Can't open current dir: $!"; 
while( readdir DH )
{
rename $_ , "\L$_"; # or , lc($_);
}
closedir DH;

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives