http://qs1969.pair.com?node_id=640856

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)

Replies are listed 'Best First'.
Re: How do I test to see if another process is writing to a file (in Windows) (!share)
by tye (Sage) 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 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)
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