Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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.

Comment on overwrite a file
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 tmp.pl and tmp.log exist) with no problems:
    perl -MFile::Copy -le 'File::Copy::copy("tmp.log", "tmp.pl");'
    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 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! :)
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 command...you 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.

      Thanks.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://444293]
Approved by RazorbladeBidet
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-07-23 09:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (138 votes), past polls