Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Determine if a file is open in another application

dboggs
New Contributor I
441 Views

My program accesses a text data file that the user may have open in a text editor. The program needs to know (1) if the user has updated the file since my program last read it, e.g. by the user SAVING it, and (2) if the user has updated the file by CLOSING it in his editor.

I can use the Fortran STAT function to get the system time of the data file, and comparing that to the previous system time enables my program to detect an updated file. This will happen in either case (1) or (2). But how can I determine simply if the data file is still open (in the users editor) or not?

I thought perhaps I could use Fortran INQUIRE (FILE=filename, OPENED=op) to do this, but my testing shows that op will only be true if filename is open to the program, and false if it is open to some other app.

0 Kudos
7 Replies
andrew_4619
Honored Contributor II
441 Views

You could try NOSHARED on the open statement (or READWRITE access) which will fail if the file is locked however you may be stuffed because some editors can open a file store it in memory or a temp file and leave the original unlocked.

 

0 Kudos
Steven_L_Intel1
Employee
441 Views

You can use GETFILEINFOQQ to get last modification date/time.

0 Kudos
dboggs
New Contributor I
441 Views

I think I did that, I just happened to use STAT instead of GETFILEINFOQQ (the documentation for STAT was easier to understand). They both give me the last modification time, but how does that tell me if the user still has the file open or not?

0 Kudos
IanH
Honored Contributor II
441 Views

Are you worried about changes being made to a file by some other program, while your program is being run?  The Windows operating system supports directory change notifications, which might help in that case.

Different editors are different, but typically a text editor will open a handle to a file, read it into memory, then close the handle to the file, before the user starts editing.  In that case, there's no extant operating system handle to the file while the user is editing.  When the user wants to save the file, the editor application will open a handle to the file again (this time for writing), update the file contents, and close the handle.  If the user closes the file in the editor without saving changes, the editor simply discards its in-memory copy of the file.

If you must manage the lifetime of an instance of a text editor, then the user needs to be using an editor that supports "this specific instance of the editor is for this file" in some way (most do, perhaps with command line options), and then you monitor the existence of the editor process that you created to edit the file.  I think we've discussed this before.

0 Kudos
dboggs
New Contributor I
441 Views

I have noticed that the Windows Task Manager (for example) can tell if a file is open, so I assume there must be an environment variable or some such thing that could possibly be accessed from within my program. Unfortunately this is beyond my pay scale. Any ideas? 

0 Kudos
dboggs
New Contributor I
441 Views

Sorry, I meant that the task manager can tell if A PARTICULAR APPLICATION is running. That might help me, because my program might (?) know what application is being used to edit the data file. I suspect, however, that the OS has some other means of knowing if a particular FILE is open, and that would be even better.

0 Kudos
Steven_L_Intel1
Employee
441 Views

Not an environment variable - it is possible to query open file handles if you have sufficient privileges. This won't tell you if the file is open in an editor, though, The ProcessMonitor application can do this too.

This old blog post shows how to do it with the Windows API (from C++).

0 Kudos
Reply