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 ?
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.
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 ?
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
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 ?
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.
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.
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
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 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.
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.
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 ?
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.
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.