Detecting if a file is open by another application
34 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!
0 comentarios
Respuesta aceptada
Walter Roberson
el 4 de Nov. de 2011
Not easily from within MATLAB. See http://www.mathworks.com/matlabcentral/answers/1030-is-there-a-way-to-detect-if-a-file-is-open-in-another-application-from-matlab
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
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.
Más respuestas (3)
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).
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
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.
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/)
0 comentarios
Ver también
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!