[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Rename Problem [Novice Question]



At 11:44 96-04-15, Russell Johannesson wrote:

>The script runs without incident, but the files
>are not renamed.

How do you know?  You don't check any error conditions!

The main problem is that readdir() only returns the leaf name of the
file.  That's not the same as the path you need from the current
directory, which is what rename() uses.

There are two ways to solve the problem:
1) chdir() to the directory in question first, or
2) use full path names.

For various reasons, #1 is better.

>opendir(FOLDER,"$dir") || die "I could not open this folder:";
    chdir( $dir ) || die( "Couldn't chdir to '$dir': $!\n" );
    opendir( FOLDER, ':' ) || die( "Couldn't opendir '$dir': $!\n" );
>@files=readdir(FOLDER);
    # no reason to not close the directory here:
    closedir(FOLDER);

>    s/(.*)\s+[-ÑÐ]\s*(.*)/$1Ñ$2/;
>    s/(.*)\s*[-ÑÐ]\s+(.*)/$1Ñ$2/;
Also, another way to write the above is:
    s/\s*[-ÑÐ]\s*/Ñ/g;
Since Perl regular expressions are "greedy".

And, most importantly, add an error test to the rename:
    rename($oldname,$_)
        || die( "Can't rename '$oldname' to '$_': $!\n" );

--Hal
Hal Wine <hal@dtor.com>     voice: 510/482-0597