Detecting if a file is open by another application
37 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
David Goldsmith
il 4 Nov 2011
Risposto: Mike Tinston
il 30 Ott 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 Commenti
Risposta accettata
Walter Roberson
il 4 Nov 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 Commenti
Walter Roberson
il 4 Nov 2011
I am going to go out on a limb and guess that the text being matched against does not include drive letters.
Più risposte (3)
Image Analyst
il 4 Nov 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
il 4 Nov 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 Commenti
Walter Roberson
il 4 Nov 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
il 30 Ott 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 Commenti
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!