[Gllug] Changing case on filenames

Simon Wilcox simonw at simonwilcox.co.uk
Wed Mar 6 11:59:05 UTC 2002


On Wed, 6 Mar 2002, Jackson, Harry wrote:

>
>
> > -----Original Message-----
> > From: Chris Ball [mailto:chris at void.printf.net]
> >
> > #!perl -w
> > opendir(DIR, "/path/to/dir");
> > @files = readdir(DIR);
> >
> > foreach $file (@files) {
> >     rename($file, (lc $file));
> > }
>
> It does but only if you are in the directory with the files. Can someone
> tell me why this is as I cannot figure out why you have to be in the same
> directory if you have already opened it in the script or am I doing
> something wrong.

readdir just returns the filename, not the full path so you either need to
do:

#!perl -w
# Explicit path example
$path = '/path/to/dir';
opendir(DIR, $path);
@files = readdir(DIR);

foreach $file (@files) {
    rename("$path/$file", $path.'/'.(lc $file));
}

Or:

#!perl -w
# Change directory example
$path = '/path/to/dir';
opendir(DIR, $path);
@files = readdir(DIR);

chdir $path;
foreach $file (@files) {
    rename($file, (lc $file));
}

Depending on your coding preference.

Simon.

-- 
"Is this a sexytune Mrs Badcrumble?"



-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list