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

I have a process that is writing PDFs to a directory. I have a Perl process that looks at this dorectory and moves the files to another directory. I am using the move command in File::Copy to move the files, but sometimes it moves a partially written file, resulting in an error on the other end. What is the best way to make sure a file is fully written before trying to move it? I should add that this is happening in Windows and I don't have control of the process that his copying the file in. Many Thanks...
  • Comment on File::Copy moving halfway written files

Replies are listed 'Best First'.
Re: File::Copy moving halfway written files
by mikeraz (Friar) on Apr 13, 2011 at 15:21 UTC

    This is discussed at Detecting An Open File. Does that Q&A not answer your questionn?


    Be Appropriate && Follow Your Curiosity
Re: File::Copy moving halfway written files
by JavaFan (Canon) on Apr 13, 2011 at 15:20 UTC
    Cooperate with the process that's doing the copying. For instance, by using a lock, or a semaphore file.
Re: File::Copy moving halfway written files
by Ratazong (Monsignor) on Apr 13, 2011 at 15:22 UTC
    The standard way for this would be to use a semaphore.
    • Your writing process could create an additional file after writing the.pdf-file.
    • Your copying process would only copy the .pdf-file if both files are available
    HTH, Rata
Re: File::Copy moving halfway written files
by choroba (Cardinal) on Apr 13, 2011 at 16:13 UTC
    When creating the file, use a different filename. Change it to the one awaited by File::Copy once the file is complete.
Re: File::Copy moving halfway written files
by mikeraz (Friar) on Apr 13, 2011 at 16:22 UTC

    Rossco, you didn't mention whether or not you have any control over the process that is creating the file you wish to copy. Three of four responses so far depend on you having that control. Could you please clarify this point?


    Be Appropriate && Follow Your Curiosity
Re: File::Copy moving halfway written files
by GrandFather (Saint) on Apr 14, 2011 at 05:28 UTC

    A technique that may help is to create a list of files with sizes and time stamps. Check the list every few seconds (or whatever interval may be appropriate), and only start copying files that haven't changed size within some appropriate time window.

    btw, if you make substantive changes to your node please indicate that you have done so.

    True laziness is hard work
Re: File::Copy moving halfway written files
by Anonymous Monk on Apr 13, 2011 at 21:35 UTC
    I don't have control of the process that his copying the file in. I am using Windows. Many Thanks...