(Winows) Finding out which process has locked a file.

C

CinnamonDonkey

Hi all,

Does anyone know how I can programatically find out which process
(resolved to human friendly string, i.e. executable) has a lock on a
file.

I have a script running which occassionally fails because it is trying
to delete a file in use by another process. When this happens I want
it to log which process has the lock.

Since it tends to happen when I am never near the computer to catch
it! :(

Cheers,
Shaun
 
E

Emile van Sebille

On 5/13/2009 2:20 AM CinnamonDonkey said...
Hi all,

Does anyone know how I can programatically find out which process
(resolved to human friendly string, i.e. executable) has a lock on a
file.

I have a script running which occassionally fails because it is trying
to delete a file in use by another process. When this happens I want
it to log which process has the lock.

You might take a look at http://ccollomb.free.fr/unlocker/ -- I've not
used the command line options although they are available...

Emile
 
T

Tim Golden

CinnamonDonkey said:
Hi all,

Does anyone know how I can programatically find out which process
(resolved to human friendly string, i.e. executable) has a lock on a
file.

I have a script running which occassionally fails because it is trying
to delete a file in use by another process. When this happens I want
it to log which process has the lock.

Since it tends to happen when I am never near the computer to catch
it! :(


Usual place to look for these things is sysinternals (now part of
Microsoft). In particular, the handle util:

http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

To get the same programatically you have to delve quite deep
into the NT low-level API. Not tried it myself.

TJG
 
D

Dave Angel

CinnamonDonkey said:
Hi all,

Does anyone know how I can programatically find out which process
(resolved to human friendly string, i.e. executable) has a lock on a
file.

I have a script running which occassionally fails because it is trying
to delete a file in use by another process. When this happens I want
it to log which process has the lock.

Since it tends to happen when I am never near the computer to catch
it! :(

Cheers,
Shaun
This may depend on what version of Windows you're using, as the API's
changed in Windows 2000, and again in XP SP2, and probably again in Vista.

I don't know an API for what you want, but filemon, from
http://technet.microsoft.com/en-us/sysinternals/bb896642.aspx
can build a log of file activity, which you can examine later.


If you do find an API that works on XP and better, please let us know.
Even if you only know a function name, I'd be willing to play with it
some, to see what it might do. I'd like to find out, and would share
what I did figure out.
 
L

Lawrence D'Oliveiro

In message <787d6072-3381-40bd-
I have a script running which occaionally fails because it is trying
to delete a file in use by another process. When this happens I want
it to log which process has the lock.


Maybe there is no such process. Maybe it's just the Dimdows filesystem
randomly returning a failure on its own. How would you know?
 
D

David Lyon

In message <787d6072-3381-40bd-
I have a script running which occaionally fails because it is trying
to delete a file in use by another process. When this happens I want
it to log which process has the lock.


Maybe there is something on sysinternals.com that you can download....
 
G

Gabriel Genellina

En Thu, 14 May 2009 08:42:07 -0300, Lawrence D'Oliveiro escribió:
In message <787d6072-3381-40bd-
I have a script running which occaionally fails because it is trying
to delete a file in use by another process. When this happens I want
it to log which process has the lock.


Maybe there is no such process. Maybe it's just the Dimdows filesystem
randomly returning a failure on its own. How would you know?


Oh, c'mon, that's just nonsense and doesn't help anybody.
 
C

CinnamonDonkey

I have to say, this has got to be one of the least helpful groups I am
subscribed to.

Some interesting links...

http://support.microsoft.com/kb/218965

http://timgolden.me.uk/python/win32_how_do_i/add-security-to-a-file.html#the-short-version

http://timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html




En Thu, 14 May 2009 08:42:07 -0300, Lawrence D'Oliveiro escribió:
In message <787d6072-3381-40bd-
(e-mail address removed)>, CinnamonDonkey wrote:
I have a script running which occaionally fails because it is trying
to delete a file in use by another process. When this happens I want
it to log which process has the lock.

Maybe there is no such process. Maybe it's just the Dimdows filesystem
randomly returning a failure on its own. How would you know?

Oh, c'mon, that's just nonsense and doesn't help anybody.
 
T

Tim Golden

CinnamonDonkey said:
> I have to say, this has got to be one of the
> least helpful groups I am subscribed to.

I'm genuinely surprised to hear you say that, especially
about this thread to which you (who appear to be the OP)
have received several replies all pointing you towards
the sysinternals tools which are pretty much the de facto
standard for doing things of this type. They're not
API calls but they're a reasonable compromise between
doing some simple parsing of output and dealing with
some underdocumented and messy low-level NT APIs.

Granted there was one rather silly response but they
really are few and far between on this list I think.
(Or perhaps I just filter them out...)

Unforunately, you're perhaps adding to the not-helpfulness here
since none of those links (and I'm the author of two of them)
address the question. The original request doesn't want to
know what security is preventing you from accessing the file: it
wants to know who has a *lock* on the file, and as I pointed
out earlier, this is only accessible by some low-level API calls.

If I and others have misunderstood the original question, then
please come back and say so, but your original request seems
fairly clearly to point towards file locking and not to
security & ownership.


TJG
 
C

CinnamonDonkey

Thanx for the response Tim! :) Great site!
> I'm genuinely surprised to hear you say that...

Early morning frustration... I appologise to all... you are totally
right. Thank you to all for the responses.

The sysinternal "Handle" tool that you made reference to earlier is
definately a working solution with minimal effort and I could indeed
use this to solve the problem.

I'd very much like to understand how such a problem could be solved
programatically though even if it means using some "underdocumented
and messy low-level NT APIs". Whilst taking the simplest approach is
the quickest to an end solution... it's also the least educational.
I'm as interested in how file locking works as I am in solving the
problem.
> "you're perhaps adding to the not-helpfulness here..."

Again, a valid point and a lesson to be learned... I should actually
read the links I post before posting them! (I actually posted them so
that I could later read them at work).

Yes, this thread is specifically about who has the *lock* on a file.

Thanx TJG for the reality check ;-)

Shaun
 
T

Tim Golden

CinnamonDonkey said:
Thanx for the response Tim! :) Great site!


Early morning frustration... I appologise to all... you are totally
right. Thank you to all for the responses.

Well it's big of you to apologise. In return, I've managed to knock
up an example of determining locked file owners. But it is a tad
messy. I'll try to work it up into an annotated example, but for
now it's on pastebin:

http://python.pastebin.com/m7b4e420a

It manages to mix pywin32, ctypes and wmi over COM via my WMI module.
I may have missed some useful shortcuts but I think it does enough
to illustrate what's going on. It's mostly a translation of a
posting which I'll try to link to when I can find it again, to
give credit where it's due.

Thanx TJG for the reality check ;-)

Well I hope the above is helpful.

TJG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top