Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

File Watching in Windows

by Redjc1990 (Novice)
on Sep 10, 2007 at 22:54 UTC ( #638186=perlquestion: print w/replies, xml ) Need Help??

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

I need to copy a file once it arrives in a specific directory. Is there any elegant way to verify the file has fully "arrived?" Before I start to copy it to the next destination? I do not want to copy a partial file. I have no control over the file name and I cannot ask them to send an md5 or other file size/md5sum info. This is happening on a Windows 2K box. Thank you, Trent

Replies are listed 'Best First'.
Re: File Watching in Windows
by jfroebe (Parson) on Sep 11, 2007 at 00:24 UTC
      I wrote something recently that does this. It is not the best way but it works for me. click
Re: File Watching in Windows
by sgt (Deacon) on Sep 11, 2007 at 13:19 UTC

    Well you are describing a "broken" interface (to call it something). You cannot guarantee the copy will be ok. Imagine the other side writes to the file in chunks of 10k every twenty minutes for ...

    On the pragmatic side, the idea is simple, check the length of the file every x seconds, copy when it hasn't changed in the last n checks (y = n * x); choose n wisely ;)

    Actually on my to_do list is to check File::Monitor; might be useful to you.

    While writing this it occurs to me a variation: have the previous check of the size done by a child process, and have the child change the name to *.ok. In the main program just copy the *ok.

    cheers --stephan
Re: File Watching in Windows
by SuicideJunkie (Vicar) on Sep 11, 2007 at 13:52 UTC
    How about attempting to open (for exclusive write) the file every N seconds? When the writer closes the file and releases their lock, the open will succeed, and then you can do the copy.

      Make certain that you open it in append mode, though, or you will overwrite the data you are trying to process.


Re: File Watching in Windows
by proceng (Scribe) on Sep 12, 2007 at 04:03 UTC
    Sending applications touches a "flag" file in the same directory.
    When "filea.cmp" exists, "filea.txt" is complete.

    UPDATE: I have no control over the file name and I cannot ask them to send an md5 or other file size/md5sum info.
    If the sender can/will not tell you when the file is complete, you can only *assume* the state based on the lack of updates over a certain time period. If the sender is concerned that the information is complete when processed, they will provide a mechanism to do so.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://638186]
Approved by jfroebe
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2023-06-02 22:40 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (4 votes). Check out past polls.