Results 1 to 18 of 18

Thread: Which application has the file open ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Which application has the file open ?

    Hi all,

    Here is the deal :

    Say I have a simple text file .txt saved to disk.

    The text file could be opened by the user in NotePad, WordPad, IExplorer, MS Word etc ...

    Is there a programmatic way of knowing which application(s) has the text file currently open ?

    I don't want to iterate all running processes and check each one of them if they have the text file open.

    I am wondering it there is a way of finding out just by knowing the text file path .

    Regards.

  2. #2
    Addicted Member
    Join Date
    Jun 2018
    Posts
    189

    Re: Which application has the file open ?

    Just a quick reply.
    Probably this might help.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Which application has the file open ?

    Quote Originally Posted by PGBSoft View Post
    Just a quick reply.
    Probably this might help.
    Thanks that looks interesting and close to what I am after but for some reason it doesn't seem to work...

    MsgBox strCmdOutput returns an empty string !

    Code:
    Sub TEST()
    
        Dim wshShell  As Object, fso As Object, objExecObject As Object, strCmdOutput As String
        Const fileOrFolderPath = "C:\Users\Info-Hp\Desktop\Shared Folder\test.txt"
        
        Set wshShell = CreateObject("WScript.Shell")
        Set fso = CreateObject("Scripting.FileSystemObject")
            If (fso.FileExists(fileOrFolderPath)) Then
                Set objExecObject = wshShell.Exec("cmd /c openfiles /query /fo table | find /I """ & fileOrFolderPath & """")
                strCmdOutput = ""
                Do While Not objExecObject.StdOut.AtEndOfStream
                    strCmdOutput = strCmdOutput & objExecObject.StdOut.ReadLine()
                Loop
              MsgBox strCmdOutput  '<==Returns empty string!
            Else
                MsgBox "File or directory does not exist."
            End If
        Set fso = Nothing
        Set wshShell = Nothing
        
    End Sub
    Do I need need administrative rights in order for the code to work ?

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Which application has the file open ?

    Two things.

    First, from what I've found, using "Openfiles.exe" will only list files opened by network users. For example, if your local machine had a shared folder on it, and someone on your network had a file opened from that shared folder, it would find that. Not applicable for what you seem to be looking for.

    Second, if you open a text file in Notepad, that file doesn't remain in an "opened" state as far as the file system is concerned. The file is opened, read, and then closed in essentially 0 seconds. So, any testing of what you are trying to do by opening a text file in Notepad is never going to work, even if the code approach you are using is valid, because Notepad doesn't leave the file in an opened state.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Which application has the file open ?

    First, from what I've found, using "Openfiles.exe" will only list files opened by network users. For example, if your local machine had a shared folder on it, and someone on your network had a file opened from that shared folder, it would find that. Not applicable for what you seem to be looking for.
    How about files in a shared folder opened by another user account on same machine ?

    Second, if you open a text file in Notepad, that file doesn't remain in an "opened" state as far as the file system is concerned. The file is opened, read, and then closed in essentially 0 seconds. So, any testing of what you are trying to do by opening a text file in Notepad is never going to work, even if the code approach you are using is valid, because Notepad doesn't leave the file in an opened state.
    How about files other than text files ... Files such as MS Word doc or excel files ?

    Thanks.

  6. #6
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Which application has the file open ?

    you need hook all process,or use file drivers

  7. #7
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,647

    Re: Which application has the file open ?

    Quote Originally Posted by OptionBase1 View Post
    Two things.

    First, from what I've found, using "Openfiles.exe" will only list files opened by network users. For example, if your local machine had a shared folder on it, and someone on your network had a file opened from that shared folder, it would find that. Not applicable for what you seem to be looking for.

    Second, if you open a text file in Notepad, that file doesn't remain in an "opened" state as far as the file system is concerned. The file is opened, read, and then closed in essentially 0 seconds. So, any testing of what you are trying to do by opening a text file in Notepad is never going to work, even if the code approach you are using is valid, because Notepad doesn't leave the file in an opened state.
    You won't even catch the opening with the normal methods for things like Notepad.

    There is a way, but it's obscenely complex, there's no open source code doing it (just closed source like Nirsoft's FileActivityWatch) and the documentation doesn't come close to being adequate (NT kernel logger tracing), I can't figure it out

  8. #8
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Which application has the file open ?

    Quote Originally Posted by JAAFAR View Post
    Hi all,

    Here is the deal :

    Say I have a simple text file .txt saved to disk.

    The text file could be opened by the user in NotePad, WordPad, IExplorer, MS Word etc ...

    Is there a programmatic way of knowing which application(s) has the text file currently open ?
    May I ask why do you need this?
    What are you trying to do?

    Quote Originally Posted by JAAFAR View Post
    I don't want to iterate all running processes and check each one of them if they have the text file open.
    That would be too easy.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Which application has the file open ?

    Quote Originally Posted by fafalone View Post
    You won't even catch the opening with the normal methods for things like Notepad.

    There is a way, but it's obscenely complex, there's no open source code doing it (just closed source like Nirsoft's FileActivityWatch) and the documentation doesn't come close to being adequate (NT kernel logger tracing), I can't figure it out
    Would this also be the case with other than notepad ? like with MS Word or MS Excel files opened in their respective applications ?

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Which application has the file open ?

    Quote Originally Posted by Eduardo- View Post
    May I ask why do you need this?
    What are you trying to do?
    In fact what I am ultimately trying to do is to find out which user account has the shared file opened and therefore locking the file .

    My logic is that if there is a programmatic way of knowing which process has the file open I could then get the username from the process handle or processid (The shared file could be opened by another user account in the same machine or by another user in the network)

    If there is a way it would probably require administrative rights which would make things all the more difficult.

    Open to any suggestions.

  11. #11
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Which application has the file open ?

    Quote Originally Posted by JAAFAR View Post
    In fact what I am ultimately trying to do is to find out which user account has the shared file opened and therefore locking the file .

    My logic is that if there is a programmatic way of knowing which process has the file open I could then get the username from the process handle or processid (The shared file could be opened by another user account in the same machine or by another user in the network)

    If there is a way it would probably require administrative rights which would make things all the more difficult.

    Open to any suggestions.
    But are you aware that most of the programs won't have the file actually open, but they read the file and close it. Then, when they have to save they open it again, save, and close it?
    Only some programs, like for instance Word, will have the file locked.

    The fist situation is almost impossible to know, because even if you was able to catch that the program have read a file, you won't be able to know whether it already closed it or not.

    (BTW, I tested Nirsoft's FileActivityWatch and I get random results, a few times it detects that Notepad opened a file but most of the times it fails -I have Windows 64 bits and used the 64 bits version-. Perhaps it is due to caching, IDK)

    Anyway, if you need the second thing, for the files that are actually locked, I attach code.
    Attached Files Attached Files

  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Which application has the file open ?

    Quote Originally Posted by JAAFAR View Post
    Would this also be the case with other than notepad ? like with MS Word or MS Excel files opened in their respective applications ?
    Opening an xlsx-file in Excel creates a lock-file in the same directory ("~$MyFilename.xlsx") which has hidden-Attribute (haven't checked for system-attribute)
    copy/paste that somewhere else, and open it in notepad, and you'll find the Account-Name of the User having this file opened.
    trying to open the "real live"-lockfile gets denied with "this file is opened by another process blablabla", so no idea how to read it during runtime.
    Neverthless, it must be possible to read that file (ReadOnly-Mode?), since if two or more Users try to open the same Excel-File they get the famous notification that that file is opened by user "john Doe"
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Which application has the file open ?

    Quote Originally Posted by Eduardo- View Post
    But are you aware that most of the programs won't have the file actually open, but they read the file and close it. Then, when they have to save they open it again, save, and close it?
    Only some programs, like for instance Word, will have the file locked.

    The fist situation is almost impossible to know, because even if you was able to catch that the program have read a file, you won't be able to know whether it already closed it or not.

    (BTW, I tested Nirsoft's FileActivityWatch and I get random results, a few times it detects that Notepad opened a file but most of the times it fails -I have Windows 64 bits and used the 64 bits version-. Perhaps it is due to caching, IDK)

    Anyway, if you need the second thing, for the files that are actually locked, I attach code.
    Thanks Eduardo.

    I have translated the code to vba 64bit but i so far it is not finding the application name that has an excel file open.

    I'll give this another try and post back.

  14. #14
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Which application has the file open ?

    Quote Originally Posted by JAAFAR View Post
    Thanks Eduardo.

    I have translated the code to vba 64bit but i so far it is not finding the application name that has an excel file open.

    I'll give this another try and post back.
    I don't currently have 64-bit Office installed to test. Do you know of any Windows program that could be used use to test, that would open a file locking it? (I have 64-bits Windows). I tried Paint but it doesn't lock files, neither Wordpad nor Notepad do.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Which application has the file open ?

    Quote Originally Posted by Zvoni View Post
    Opening an xlsx-file in Excel creates a lock-file in the same directory ("~$MyFilename.xlsx") which has hidden-Attribute (haven't checked for system-attribute)
    copy/paste that somewhere else, and open it in notepad, and you'll find the Account-Name of the User having this file opened.
    trying to open the "real live"-lockfile gets denied with "this file is opened by another process blablabla", so no idea how to read it during runtime.
    Neverthless, it must be possible to read that file (ReadOnly-Mode?), since if two or more Users try to open the same Excel-File they get the famous notification that that file is opened by user "john Doe"
    I am aware of this hidden ~$xxx temporary file that gets created in the same folder. there is one exception and that is with .xls files.
    For .xls files, each time they are saved , the username gets updated in the original file.

    The issue with this is that it doesn't get you the user account name which you log into Windows with. It gives you the application username which is set by the user for the excel application.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Which application has the file open ?

    Quote Originally Posted by Eduardo- View Post
    I don't currently have 64-bit Office installed to test. Do you know of any Windows program that could be used use to test, that would open a file locking it? (I have 64-bits Windows). I tried Paint but it doesn't lock files, neither Wordpad nor Notepad do.
    No not sure about programs that lock opened files except for office programs...I'll study the code more carefully and see what I get.

    BTW, is that code supposed to work accross user accounts and accross machines over a network ? Does the code gives admin privileges\rights to the calling process ?

  17. #17
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Which application has the file open ?

    Quote Originally Posted by JAAFAR View Post
    BTW, is that code supposed to work accross user accounts and accross machines over a network ? Does the code gives admin privileges\rights to the calling process ?
    The part that enums the handles of opened files, used to work when the file was opened from another user account in the same machine in XP (I never tested the case when it was open from another computer, but if the file is locally locked, it should work I think).
    But the part that lists the running process as it is now, I think won't find the exe locking it if the exe is running in another user account.
    In an old version I had other code (that I can share if you want) for the part finding the exe, but at some point I changed it now I don't remember why. The older code, also in XP, was able to find the exe running from another user (I think I recall).

    As it is now (the code posted), it can find when a 32 bits exe, running in the same user account locks the file. That does not require admin rights.

  18. #18
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Which application has the file open ?

    I've been testing and found that:
    It works when the file is opened by 64-bits processes running in the same user with the code as it is.
    It does not detect when the file is opened from another user account of the same machine, at least in Windows 10 64-bits.
    Whether the handle does not appear on the list returned by NtQuerySystemInformation(SystemHandleInformation... or it is not able to get the file path from the handle in the GetFileFullPath function, IDK.
    I tested also running with elevated privileges with no luck.

    NtQuerySystemInformation with SystemHandleInformation is currently undocumented.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width