Detecting if a file is open by another application

34 visualizaciones (últimos 30 días)
David Goldsmith
David Goldsmith el 4 de Nov. de 2011
Respondida: Mike Tinston el 30 de Oct. de 2017
Hi! I'm using MATLAB R2011b on Windows XP SP3; is there a way to determine if a file is open by another application (if not within matlab then perhaps w/ a DOS command)? (From within my matlab ap, I'm calling another, non-matlab application which produces a file; I want my matlab ap to do a few things while it's waiting for this other ap to finish, then open up that produced file and append some stuff to it. I'm thinking if there's some way to detect that the file is still open, I can just while loop until that's not true, then open it and proceed...)
Thanks!

Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Nov. de 2011
You may wish to have a look at ProcessExplorer
A typical mechanism in MS Windows is to simply try the fopen(), in a try/catch block, under the assumption that the file is local or on a Microsoft shared file system (not an NFS file system!), and under the assumption that the application that opened the file for writing used the default open() settings that lock the file when write permission is requested. This method is unreliable and can suffer from "race conditions", but you might find it "good enough" for you.
  7 comentarios
Walter Roberson
Walter Roberson el 4 de Nov. de 2011
I am going to go out on a limb and guess that the text being matched against does not include drive letters.
David Goldsmith
David Goldsmith el 5 de Nov. de 2011
Right idea, wrong end of the beast: it's not the drive letter it minds, it was the trailing file separator I was including--at least now I'm getting some results, which is progress, but...
The results I'm getting stop at a folder--presumably ones containg open files?--but don't give the actual names of the open files, e.g.:
C:\Data\My Documents\JobRelated\MWDMS\MWDataMgmt\RT\Telnet>handle -a "C:\Data\My Documents\JobRelated\MWDMS\MWDataMgmt\RT\Telnet"
Handle v3.46
Copyright (C) 1997-2011 Mark Russinovich
Sysinternals - www.sysinternals.com
explorer.exe pid: 3164 type: File 118C: C:\Data\My Documents\JobRelated\MWDMS\MWDataMgmt\RT\Telnet
cmd.exe pid: 5812 type: File C: C:\Data\My Documents\JobRelated\MWDMS\MWDataMgmt\RT\Telnet
MATLAB.exe pid: 4680 type: File AD8: C:\Data\My Documents\JobRelated\MWDMS\MWDataMgmt\RT\Telnet\dist
MATLAB.exe pid: 4680 type: File AEC: C:\Data\My Documents\JobRelated\MWDMS\MWDataMgmt\RT\Telnet
handle.exe pid: 2448 type: File C: C:\Data\My Documents\JobRelated\MWDMS\MWDataMgmt\RT\Telnet
As you can see from my command prompt, "C:\Data\My Documents\JobRelated\MWDMS\MWDataMgmt\RT\Telnet" is a folder, not a file. The only thing I don't understand in these results is the part between "File" and the path, e.g., the 118C in the first result. These pieces look suspiciously like hex numbers--are they some cryptic specifier of the actual file in the indicated directory? For what it's worth, when I use the -p modifier with a process, which lists handles associated w/ that process, which in turn can be specified either by number or program name, the results are similar, except that I get a mix of folders and files--but none of the files that I see to be associated w/ the application!
:-(

Iniciar sesión para comentar.

Más respuestas (3)

Image Analyst
Image Analyst el 4 de Nov. de 2011
Not that I know of, other than to keep calling dir() on it and checking the date stamp, or trying to open it by looping until you don't have an exception thrown (in the event that some program put a lock on that file, which not every program does).
  1 comentario
David Goldsmith
David Goldsmith el 4 de Nov. de 2011
Thanks IA, I thought of the latter one after I posted, but not the first one (which strikes me as a little less robust--how many times in a row should one find the date stamp unchanged before one decides the other program is done w/ it?--but at least it doubles my arsenal :-)). I didn't realize that locking the file was not a uniform practice however, so thanks for the heads-up there (the good news is I wrote the other ap, so I think I should be able to garantee that the file will be locked until it is done being written to). Thanks again!

Iniciar sesión para comentar.


Daniel Shub
Daniel Shub el 4 de Nov. de 2011
Since you wrote the other app that opens the file, there are lots of ways of doing it (none of them are particularly good). I think the best answer depends on how much you want to modify the other app, what language that app is written in, and how tightly coupled the other app and your MATLAB app are.
  3 comentarios
Walter Roberson
Walter Roberson el 4 de Nov. de 2011
Python documentation for open() and os.open() is silent on this issue, which is not surprising as Microsoft's own documentation is silent on this issue.
http://docs.python.org/library/functions.html#open
http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx
http://packages.python.org/lockfile/lockfile.html
But notice that the above package is for advisory locks -- mandatory locks are not generally implemented in Unix.
One thing I can say with certainty is that using 'a+' mode is *not* a mechanism for preventing or denying shared access: the '+' indicators have entirely different purposes.
David Goldsmith
David Goldsmith el 5 de Nov. de 2011
Interesting result on this: MATLAB, using fopen(..., 'at') appears to be able to open the file while it's still open by the other process--as evidenced by returning 3 for a file ID--and fclose doesn't stop the other app from writing to it, but the thing I fprintf to it between opening and closing never shows up anywhere in the file, i.e., not after the fclose, nor after the other app is done with it either! It appears that I'll either need to figure out how to use handle for this, or try IA's date stamp method.

Iniciar sesión para comentar.


Mike Tinston
Mike Tinston el 30 de Oct. de 2017
In a Linux based system you can try:
[stat, struct] = system(sprintf('lsof %s', filename));
If struct is empty, not process has that file open. I've read that "netstat -b" is the windows equivalent. (https://www.reddit.com/r/sysadmin/comments/1kzrrs/windows_equivalent_of_lsof_i/)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by