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

How do I test to see if another process is writing to a file (in Windows)

by jmurphy (Novice)
on Sep 25, 2007 at 04:36 UTC ( #640856=perlquestion: print w/ replies, xml ) Need Help??
jmurphy has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm racking my tiny brain and shockingly still can't come up with an elegant solution for the following problem:

Is it possible to determine whether an external process is writing to file on Windows? I'm writing a Perl application that polls the file system. It looks for files with a certain extension (.log) and if it finds one it takes action. However, it shouldn't take action until the external application has finished writing the log file. Therefore the polling application needs to check and see of the external process is still writing to the file.

Is there an easy way to do this? I've read the archives and I see that some people have used stat in order to check for changes in the size and/or modified date of a file. However, this seems so primitive. Any elegant solutions out there?

Thanks so much,
Jay

Comment on How do I test to see if another process is writing to a file (in Windows)
Re: How do I test to see if another process is writing to a file (in Windows)
by mr_mischief (Monsignor) on Sep 25, 2007 at 05:50 UTC
    Checking to see if the file is open and then trying to open it introduces a race condition. It's better to just try to open the file, then handle any errors from not being able to do so.
Re: How do I test to see if another process is writing to a file (in Windows) (!share)
by tye (Cardinal) on Sep 25, 2007 at 06:16 UTC

    Sure, just open the file w/o sharing "write" access. Such an open will fail if something currently has the file open for writing. Use Win32API::File::createFile() to easily do this.

    - tye        

      Tye,

      Thanks for the pointer. This is exactly what I needed. However, I'm having problems using createFile. If I open the file "a.out" in an editor (Textpad or Notepad) and then run the following:

      #!/usr/bin/perl -w use strict; use Win32API::File 0.08 qw( :ALL ); my $status = createFile( "a.out", "w ke", "r" ) or die "Can't get exc +lusive access to file: $^E\n"; if ( $status ) { print "Got it!\n"; }
      $status is always true. I can't get createFile to "die". Any ideas?

      Thanks,
      Jay

        I guess the interactions are a bit different than my expectations. I get the same results. Worse, once I get the exclusive access, the other process still has the file open for write access but its attempts to write to the file fail.

        So, instead, don't share "r" access and you can tell if anything has the file open at all, it seems.

        - tye        

        I'm not so sure that having a file in Notepad marks the file in use. I can open the same file over and over again in Notepad. Did you try something with more "bloat" like Word?


        I humbly seek wisdom.
Re: How do I test to see if another process is writing to a file (in Windows)
by blahblahblah (Priest) on Sep 25, 2007 at 23:34 UTC
    How much control do you have over this external application? I've so far avoided this type of problem by making my logs have date stamps, like 20070925.log. Then I'll hold off on post-processing for a day, and I can rely on the file not being in use.

    Joe

      (Especially when not on Windows) I usually have the writing process write to a *.tmp file and then rename the file after they are done writing it.

      - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-08-29 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls