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

Discussion in 'Python' started by CinnamonDonkey, May 13, 2009.

  1. 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
    CinnamonDonkey, May 13, 2009
    #1
    1. Advertising

  2. 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
    Emile van Sebille, May 13, 2009
    #2
    1. Advertising

  3. CinnamonDonkey

    Tim Golden Guest

    CinnamonDonkey wrote:
    > 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
    Tim Golden, May 13, 2009
    #3
  4. CinnamonDonkey

    Dave Angel Guest

    CinnamonDonkey wrote:
    > 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.
    Dave Angel, May 13, 2009
    #4
  5. Re: (Windows) Finding out which process has locked a file.

    In message <787d6072-3381-40bd-
    >, 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?
    Lawrence D'Oliveiro, May 14, 2009
    #5
  6. CinnamonDonkey

    David Lyon Guest

    Re: (Windows) Finding out which process has locked a file.


    > In message <787d6072-3381-40bd-
    > >, 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 something on sysinternals.com that you can download....
    David Lyon, May 14, 2009
    #6
  7. Re: (Windows) Finding out which process has locked a file.

    En Thu, 14 May 2009 08:42:07 -0300, Lawrence D'Oliveiro escribió:
    > In message <787d6072-3381-40bd-
    > >, 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.

    --
    Gabriel Genellina
    Gabriel Genellina, May 15, 2009
    #7
  8. Re: (Windows) Finding out which process has locked a file.

    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




    On 15 May, 03:47, "Gabriel Genellina" <> wrote:
    > En Thu, 14 May 2009 08:42:07 -0300, Lawrence D'Oliveiro escribió:
    >
    > > In message <787d6072-3381-40bd-
    > > >, 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.
    >
    > --
    > Gabriel Genellina
    CinnamonDonkey, May 15, 2009
    #8
  9. CinnamonDonkey

    Tim Golden Guest

    Re: (Windows) Finding out which process has locked a file.

    CinnamonDonkey wrote:
    > 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...)

    > 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


    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
    Tim Golden, May 15, 2009
    #9
  10. Re: (Windows) Finding out which process has locked a file.

    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



    On 15 May, 08:50, Tim Golden <> wrote:
    > CinnamonDonkey wrote:
    >
    >  > 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...)
    >
    > > Some interesting links...

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

    >
    > >    http://timgolden.me.uk/python/win32_how_do_i/add-security-to-a-file.h...

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

    >
    > 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
    CinnamonDonkey, May 15, 2009
    #10
  11. CinnamonDonkey

    Tim Golden Guest

    Re: (Windows) Finding out which process has locked a file.

    CinnamonDonkey wrote:
    > 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.


    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
    Tim Golden, May 15, 2009
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith
    Replies:
    0
    Views:
    518
    Keith
    Oct 1, 2003
  2. Praveena
    Replies:
    0
    Views:
    311
    Praveena
    Jun 24, 2004
  3. Manish Jethani

    Finding out which thread has interrupted

    Manish Jethani, Jan 7, 2004, in forum: Java
    Replies:
    2
    Views:
    317
  4. Felipe
    Replies:
    1
    Views:
    330
    Laurent Bugnion
    Aug 11, 2006
  5. Keith
    Replies:
    2
    Views:
    147
    Keith
    Oct 2, 2003
Loading...

Share This Page