Hi,

I am looking for a solution to find out if a file is opened in a
windows (NT oder W2k or XP) application.

For office documents it seems to be clear: trying to open the document
exclusively - if that is succesful, the document is not opened.

But what can I do if I want to know if, for example, a text file is
opened in notepad? If you want you can rename or delete the file while
it is opened in notepad. I think it's the same for documents in wordpad
or images in paint. In this case the file always can be opened
exclusively.

Maybe it is possible to run a VBScript that checks the running
aplications and their window titles? But thinking about that I see two
problems:

1. consider 2 text files c:\dir1\test.txt and c:\dir2\test.text (same
file names but different directories. If both of these files are opened
in notepad I would have two windows using the same title "test.txt -
Notepad". Is there a chance to find out which file is opened in which
notepad?

2. Consider applications that open all documents in one window. In this
case you will have only one application window using different window
titles depending on the document that is in front.

I hope, someone has an idea how to find a solution. Thanks very much in
advance!

Frank

Re: Checking if a file is opened by mr_unreliable

mr_unreliable
Wed Dec 28 11:46:20 CST 2005

hi Frank,

I think you are going to have trouble with this one, especially
if you want to stick with "pure" script.

Windows is a "sharing" system, allowing multiple apps to access
the same file. As for the ms Office apps, I don't know for sure
-- but I suspect they are using the LockFile/UnlockFile api calls
to keep a second app away from the file they are working on.

There are some utilities that do what you are looking to do, but
these are not written in script -- far from it. For example, take
a look at sysinternals file monitoring utility, "FileMon v7.02",
found here:

http://www.sysinternals.com/FileAndDiskUtilities.html

Those sysinternals guys (and gals?) are very tricky with what
they do, and while you get the binary app, there is no source
code available, so I can't say for sure how they do it. My best
guess is that they are "hooking" into the system message stream,
and keeping track of the messages relating to file usage.

I suspect you are going to have to do something extreme like
that to keep track of who is using what file.

mfg, jw

FrankWinkler wrote:
> Hi,
>
> I am looking for a solution to find out if a file is opened in a
> windows (NT oder W2k or XP) application.
>
> For office documents it seems to be clear: trying to open the document
> exclusively - if that is succesful, the document is not opened.
>
> But what can I do if I want to know if, for example, a text file is
> opened in notepad? If you want you can rename or delete the file while
> it is opened in notepad. I think it's the same for documents in wordpad
> or images in paint. In this case the file always can be opened
> exclusively.
>
> Maybe it is possible to run a VBScript that checks the running
> aplications and their window titles? But thinking about that I see two
> problems:
>
> 1. consider 2 text files c:\dir1\test.txt and c:\dir2\test.text (same
> file names but different directories. If both of these files are opened
> in notepad I would have two windows using the same title "test.txt -
> Notepad". Is there a chance to find out which file is opened in which
> notepad?
>
> 2. Consider applications that open all documents in one window. In this
> case you will have only one application window using different window
> titles depending on the document that is in front.
>
> I hope, someone has an idea how to find a solution. Thanks very much in
> advance!
>
> Frank
>

Re: Checking if a file is opened by FrankWinkler

FrankWinkler
Thu Dec 29 02:30:49 CST 2005

Hi jw!

Thanks for your reply. I think you're right. It seems to be not so easy
to find out directly which file is opened by which application using a
pure script.

I think the easiest solution would be to use a script that returns a
list that is similar to the list showed on the first tab of Windows
Task Manager. In this list not only the running applications are shown,
the opened files are displayed, too. By calling such a script maybe
every 2 seconds and looking in the returned list for the interesting
file(s), it should be possible to find out if a file is opened or
closed. The delay (maybe 2 seconds) should'nt be that problem.

Of course I looked for a a solution like that. Unfortunatelly I found
only lots of scripts for getting the running processes (like second tab
in Task Manager) using Win32_process. Maybe someone here knows a
solution (script) that returns that applictaion list?

Frank


Re: Checking if a file is opened by mr_unreliable

mr_unreliable
Thu Dec 29 13:56:07 CST 2005

hi again Frank,

I may have a more appropriate sysinternals utility for you.

Take a look at the "Process Explorer v9.25" found here:

http://www.sysinternals.com/ProcessesAndThreadsUtilities.html

--- <quote> ---
Find out what _FILES_ (emphasis mine), registry keys and
other objects processes have open...
--- </quote> ---

I am not quite up to date with the latest version, but the v8.xx
version has an upper pane showing the processes, and a multi-
purpose "lower pane". If you configure the lower pane to show
"system handles", and click on a particular process, then you get
a listing of system resource "handles" in the lower pane.

If you peruse through the handle listing, you will see lots of
"techy" stuff, (like mutexes and semaphores), but also the handles
of open files. In other words it gives you a listing of files
associated with each process (i.e., each app).

Frankly, I have looked at a lot of vb source code, and not seen
anything that would produce a listing of system resource handles
like the sysinternals process explorer produces. I strongly suspect
that Mark Russinovich and Bryce Cogswell have a deep-throat-
Microsoftie" who is giving them information about windows internal
tables, or else Mark & Bryce have spent weeks, months, even YEARS
combing through hex dumps "reverse engineering" the system internal
tables to get that listing of handles.

The point is, I don't think you are going to easily find anything
like what Mark and Bryce have coded up. Certainly not in any
scripting language or 3rd-party objects, and probably not even in
any vb code.

cheers, jw



FrankWinkler wrote:
> Hi jw!
>
> Thanks for your reply. I think you're right. It seems to be not so easy
> to find out directly which file is opened by which application using a
> pure script.
>
> I think the easiest solution would be to use a script that returns a
> list that is similar to the list showed on the first tab of Windows
> Task Manager. In this list not only the running applications are shown,
> the opened files are displayed, too. By calling such a script maybe
> every 2 seconds and looking in the returned list for the interesting
> file(s), it should be possible to find out if a file is opened or
> closed. The delay (maybe 2 seconds) should'nt be that problem.
>
> Of course I looked for a a solution like that. Unfortunatelly I found
> only lots of scripts for getting the running processes (like second tab
> in Task Manager) using Win32_process. Maybe someone here knows a
> solution (script) that returns that applictaion list?
>
> Frank
>

Re: Checking if a file is opened by FrankWinkler

FrankWinkler
Fri Dec 30 06:27:58 CST 2005

Hi again JW,

thanks a lot for your help. I had a look at the decription of Process
Explorer and I think it's a great tool. But unfortunatelly it isn't
that what I'm looking for. To solve my problem I can't use a tool that
has a very nice GUI. I need something, that generates some kind of file
that I can read with my application. This could be a script that I
start from my application periodically or a little application that
runs in the background and writes such a file peridically. Everything
would be ok - but it must work in the background...

Beest regards!
Frank


Re: Checking if a file is opened by scattered

scattered
Fri Dec 30 07:56:01 CST 2005


FrankWinkler wrote:
> Hi again JW,
>
> thanks a lot for your help. I had a look at the decription of Process
> Explorer and I think it's a great tool. But unfortunatelly it isn't
> that what I'm looking for. To solve my problem I can't use a tool that
> has a very nice GUI. I need something, that generates some kind of file
> that I can read with my application. This could be a script that I
> start from my application periodically or a little application that
> runs in the background and writes such a file peridically. Everything
> would be ok - but it must work in the background...
>
> Beest regards!
> Frank

Hi

Just a random thought: you might be able to write a batch file for this
(which could be called by a vbscript). I have been reading through
"Windows XP: Under the Hood" by Brian Knittel recently, a book which
covers both VBScript and batch programming and have been surprised to
discover that there are certain tasks that are actually easier with
batch files than scripts. Your question has a similar flavor to some of
these. I personally have no idea how to solve it, but I have been very
impressed with the expertise of the regular posters in
alt.msdos.batch.nt - maybe you could repost your question there. You
might get lucky.

-John Coleman