Smaug has asked for the wisdom of the Perl Monks concerning the following question:

Hello all-knowing monks,

Please help me out by explaining the following...

use strict; use File::Copy; my $PPath=($ENV{Project}); my $LogPath=("$PPath/midnight_reset/log"); my $CPath=($ENV{CONFIG_DIR}); $CPath=~s/\\/\//g; my $file; my $logpath=("$CPath/Agents/log"); opendir (DIR, $logpath) || die "Cannot open dir $logpath: $!"; my @files=grep(/\.log$/i, readdir (DIR)); foreach $file (@files) { unless (copy ("$logpath/$file", "$logpath/$file.old")) { print "Fail to rename $file\n"; } } closedir (DIR);


As you can see from above, I'm trying to back up log files to *.old. In the section where I do the copy, I had tried using 'rename' and it failed. I then tried File::Copy and used 'move' and it also failed.

I have now got it working, but was wondering if somebody could help me by explaining why the others did not work?

I'm not sure if it matters, but this is running on Win32.

Thanks!!

Replies are listed 'Best First'.
Re: rename and move fail?
by tachyon (Chancellor) on Oct 01, 2003 at 07:13 UTC

    Perhaps you might save us the trouble and let Perl explain the problem to you:

    # like all good Perl programmers [insert name here] always # tests the return value of functions like open, unlink, # rename etc and includes the error message kindly put by # Perl into $! so the reason for a failure is made # immediately obvious in all its gory diagnostic detail..... rename $old, $new or die "Can't rename Perl say reason $!\n";

    Permission or path if you want the short answer. $! if you want the exact details.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Ah ha!!
      'Access Denied'

      Thanks tachyon!