Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

overwrite a file

by Anonymous Monk
on Apr 01, 2005 at 21:25 UTC ( #444293=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I want to copy a file over to another directory and if that file exists, I would like to overwrite the file. A copy command:

copy ( source, destination );

But, the copy command does not overwrite the file if it already exists. Is there a way I can force the overwrite?

Thanks in advance.

Replies are listed 'Best First'.
Re: overwrite a file
by RazorbladeBidet (Friar) on Apr 01, 2005 at 21:38 UTC
    I'm assuming you're using File::Copy.

    What are you trying? I can do the following (where both and tmp.log exist) with no problems:
    perl -MFile::Copy -le 'File::Copy::copy("tmp.log", "");'
    Do you maybe not have write permissions?
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
Re: overwrite a file
by bcole23 (Scribe) on Apr 01, 2005 at 21:43 UTC
    Your problems are probably due to file permissions or something else having a lock on the file.

    If you can't copy over it, then you could check for locks, sleep, and retry for a while. If you don't know or have control over the files that you're copying over, you may want to check permissions on the files first and process accordingly.
      Thank you all for your responses.

      I am trying to use File::Copy are probably right...I might not have permissions to change the file. If I don't have permission to change the file, I might not be able to overwrite. I'll go ahead and implement changing the permissions for the files.


Re: overwrite a file
by punch_card_don (Curate) on Apr 01, 2005 at 22:59 UTC
    Do you mean you're trying to create a "copy" subroutine to use in a program? Something like:
    sub copy( source, destination ) { open SOURCEFILE "source"; open DESTINATIONFILE "destination"; while (<SOURCEFILE>) { print DESTINATIONFILE ; } close DESTINATIONFILE; close SOURCEFILE; }

    If so, then maybe the problem is in how you're opening the destination file.

    open FILE "<$file";
    to read a file
    open FILE ">$file";
    to write to a file, creating it if necessary
    open FILE ">>$file";
    to append to a file, creating it if necessary

    Forget that fear of gravity,
    Get a little savagery in your life.

Re: overwrite a file
by tlm (Prior) on Apr 01, 2005 at 21:41 UTC

    Update: Please disregard; wrong info. My bad.

    the lowliest monk

      He said, "copy a file over to another directory", so I believe it is not the same file. <Update> and so please disregard this too! :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://444293]
Approved by RazorbladeBidet
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2018-06-25 09:09 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.