Detecting if a file is open by another application

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!

 Accepted Answer

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 Comments

Thanks, that thread didn't turn up in my search (perhaps my word choice was not general enough, I don't remember exactly what I searched for). I have/am somewhat familiar w/ ProcessExplorer--does it have a command line interface?
POSIX semaphores: http://www.mathworks.com/matlabcentral/fileexchange/32489
Example of using a file as a semaphore: http://www.mathworks.com/matlabcentral/fileexchange/13775-multicore-parallel-processing-on-multiple-cores
Package that implements file semaphores as part of its work: http://www.mathworks.com/matlabcentral/fileexchange/18593-differential-evolution
Sorry, I do not know if process explorer has a command line interface.
Actually, the link you gave me led me to something better: Handle (http://technet.microsoft.com/en-us/sysinternals/bb896655). It's exactly what I need; the only drawback: user must have admin privileges. But I can put it in a try/catch, w/ the less robust keep-opening-'til-something-other-than- -1-is-returned tried if Handle throws an exception. Thanks for the tip!
Oooops, may have spoken to soon: I can't figure out how to use it correctly, I guess. handle by itself works (even though, I subsequently discovered, I'm not part of the administrator group on my computer) but when I try to use the handle name syntax--where for name I've tried both complete path (in quotes) file names and program names--I get "No matching handles found." The help for the name parameter says: "Search for handles to objects with <name> (fragment accepted)"--what do you suppose they could mean by "objects" if neither files nor programs?
I am going to go out on a limb and guess that the text being matched against does not include drive letters.
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!
:-(

Sign in to comment.

More Answers (3)

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 Comment

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!

Sign in to comment.

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 Comments

OK, more details: the other app is a py2exe-d Python ap and the file in question is opened w/ a simple open(fn, 'w'). Python's help for open (file, actually) indicates that one must afix a '+' to the mode to make it available for reading and writing, but, AFAIC, that doesn't answer the questions: a) does a straight-up 'w' lock the file, and b) if so, w.r.t. all apps, or just python apps, or just the python app that opened the file? If anyone knows the answer...thanks in advance!
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.
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.

Sign in to comment.

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/)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!