Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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 ( [id://640866]=note: print w/replies, xml ) Need Help??


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

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        

  • Comment on Re: How do I test to see if another process is writing to a file (in Windows) (!share)

Replies are listed 'Best First'.
Re^2: How do I test to see if another process is writing to a file (in Windows) (!share)
by jmurphy (Acolyte) on Sep 25, 2007 at 07:37 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://640866]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found