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

Hi Monks!

Trying to rename a file, but I get "Cannot rename file: No such file or directory at ..., <$fh> ...".
A file gets created with the new name, I see in the "tempdir", cant understand the reason for the error. Here is a snipped of the code to see if someone could explain the reason.
... my $attach = "file.png"; my @fh = split(/\\/, $attach); my $last_elmnt = @fh; my $filename = $fh[$last_elmnt-1]; my $tempfile = "tempdir/$filename"; my $new_filename = "allfiles.png"; open( ATTA, ">$tempfile" ) || die "Can't open $tempfile: $!"; binmode ATTA; while ( <$attach> ) { rename $tempfile, "tempdir/".$new_filename or die "Cannot rename f +ile: $!"; } print ATTA; } close ATTA; ...

Thanks for looking!!

Replies are listed 'Best First'.
Re: File "rename" error in renaming a file.
by GrandFather (Saint) on Oct 11, 2022 at 20:54 UTC

    Elaborating on from Marshall's comment, it looks like you found some code in a dumpster, dusted it off, shuffled the lines a little and expected it to perform some, largely unspecified, task. Maybe this is because you have stripped out code you didn't think relevant before you showed it to us? In any case, as suggested by hv, a SSCCE would help a lot.

    Your code as presented doesn't do as you claim. The rename line in the loop doesn't get hit because the loop body is not executed. In fact the spurious } at the end of the rename line means the code doesn't even compile. Maybe something closer to your intent is:

    use strict; use warnings; use File::Basename; use File::Spec; my $attach = "file.png"; my $filename = File::Basename::fileparse($attach); my $tempfile = File::Spec->catfile("tempdir", $filename); my $new_filepath = File::Spec->catfile("tempdir", "allfiles.png"); rename $tempfile, $new_filepath or die "Cannot rename file $tempfile to $new_filepath: $!";
    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: File "rename" error in renaming a file.
by Marshall (Canon) on Oct 11, 2022 at 20:23 UTC
    First suggestion would be to put more info in your error message:
    rename $tempfile, "tempdir/".$new_filename or die "Cannot rename $tempfile to tempdir/$new_filename" $!";
    You are renaming an open file. This is indeed allowed, but in this code, I see no need and would avoid that.
    while ( <$attach> ) makes no sense because $attach is a text string - what you are trying to do is not at all clear.

    Update: Also add use strict; use warnings to your code. If you had done that you will see that while ( <$attach> ) produces the error of attempted read on an unopened file handle.

Re: File "rename" error in renaming a file.
by hv (Prior) on Oct 11, 2022 at 19:59 UTC

    I note that you are performing the rename inside a loop, I suspect you simply need to move it outside the loop.

    If that's not it, it would really help to have more information: a short, self-contained, correct example (see SSCCE) would be a great start.

      I tried moving it just outside of the "while" loop:
      ... open( ATTA, ">$tempfile" ) || die "Can't open $tempfile: $!"; binmode ATTA; rename $tempfile, "tempdir/".$new_filename or die "Cannot rename fil +e: $!"; } while ( <$attach> ) { ...

      Got this error : " Error calling upload_file: '' is not a filehandle ".