For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > rename all files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author rename all files
Xuantl

2005-02-20, 8:55 am

Hi all,

I wan't to change name of all the files in a directory(incluing sub
directorys) to lowercase.

the code:
-------------------------------------------
use File::Find;
$rootDir='.';
find (\&lowerCase, $rootDir);
find (sub{print "$File::Find::name\n"}, $rootDir);

sub lowerCase {
$oldName = "$File::Find::name";
$newName = "\L$File::Find::name";
print (rename ($oldName, $newName));
print " rename result: \"$oldName\" - \"$newName\"\n";
};
-------------------------------------------

But it only works for files in the top directory("."), and no effect to
all other files in sub directorys. Any help?
I'm a beginner. If it's a stupid question for you, please don't hit me!

Edward Wijaya

2005-02-20, 8:55 am

On Sat, 19 Feb 2005 16:40:43 +0800, xuantl <xuantl@gmail.com> wrote:

> Hi all,


Hi

>
> I wan't to change name of all the files in a directory(incluing sub
> directorys) to lowercase.


[snip]

> But it only works for files in the top directory("."), and no effect to
> all other files in sub directorys. Any help?
> I'm a beginner. If it's a stupid question for you, please don't hit me!
>

Try using finddepth() instead of find()
With find(), you're going to get DIR/ DIR/file1 DIR/file2
If you rename DIR to dir, DIR/file1 no longer exists.



But finddepth returns directory contents before the directories themselves.
BTW your code would have been clear, btw, if you'd said, for example:
rename $old, $new or warn "rename: $!\n";
also, "$new = lc $old" is more readable than $new = "\L$old"

My suggestion is untested, it'll be nice if you let us know if it works ;-)

HtH


--
Edward WIJAYA
Singapore
John W. Krahn

2005-02-20, 8:55 am

xuantl wrote:
> Hi all,


Hello,

> I wan't to change name of all the files in a directory(incluing sub
> directorys) to lowercase.
>
> the code:
> -------------------------------------------
> use File::Find;
> $rootDir='.';
> find (\&lowerCase, $rootDir);
> find (sub{print "$File::Find::name\n"}, $rootDir);
>
> sub lowerCase {
> $oldName = "$File::Find::name";
> $newName = "\L$File::Find::name";
> print (rename ($oldName, $newName));
> print " rename result: \"$oldName\" - \"$newName\"\n";
> };
> -------------------------------------------
>
> But it only works for files in the top directory("."), and no effect to
> all other files in sub directorys. Any help?
> I'm a beginner. If it's a stupid question for you, please don't hit me!


I assume that you are not trying to do this in DOS/Windows as the FAT file
system is case insensitive and it won't work (don't know about NTFS.)

The variable $File::Find::name contains the complete path of the file
including the directory name so if any of the directory names have upper case
letters then you are trying to move the files to a directory that does not
exist. You should just convert the file names to lower case.

sub lowerCase {
$oldName = $_;
$newName = lc;
print rename( $oldName, $newName ),
qq( rename result: "$oldName" - "$newName"\n);
}



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com