
Test for open files
Moderators: JRL, Dorian (MJT support)
Test for open files
Is it possible to loop through a files and return an error if any are in an open state. 

JOE
Yes, you can do it with VBScript file system object or just try to write a blank line to the file with WriteLn. If the file is locked/open WriteLn will return an error code.
MJT Net Support
[email protected]
[email protected]
There's no such thing as a file open status. There is such a thing as a file lock however. But bear in mind that some applications such as Notepad open the file, read it into memory and then release the lock. So while the file would appear "open" as far as the user is concerned, the file is not locked.
The VBScript solution is the same as the WriteLn one - you attempt to open the file for appending and if an error occurs you know it is locked:
VBSTART
Const ForAppending = 8
Function IsFileLocked(filename)
Set oFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set f = oFSO.OpenTextFile(sLogFile, ForAppending, True)
If Err.Number = 70 Then
'Permission denied error
IsFileLocked = true
Else
f.Close
IsFileLocked = false
End if
On Error GoTo 0
End Function
VBEND
VBEval>IsFileLocked("d:\myfile.txt"),flock
The VBScript solution is the same as the WriteLn one - you attempt to open the file for appending and if an error occurs you know it is locked:
VBSTART
Const ForAppending = 8
Function IsFileLocked(filename)
Set oFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set f = oFSO.OpenTextFile(sLogFile, ForAppending, True)
If Err.Number = 70 Then
'Permission denied error
IsFileLocked = true
Else
f.Close
IsFileLocked = false
End if
On Error GoTo 0
End Function
VBEND
VBEval>IsFileLocked("d:\myfile.txt"),flock
MJT Net Support
[email protected]
[email protected]
tested and does not seem to work
I tested the script above and it does not seem to work, I tried a notepad file which I understated is in memory so I tried an Excell file the script said I could write to it yet when opened on another machine the message came up could not write to the file as it was open. Is there anyway of modifing the script so that it works on all files instead of text files and that it always works?
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Nope. As I said there is no such status as a file being locked. Notepad does not lock files. You can have a file open in notepad and still edit it elsewhere. I believe Excel uses a propriatary approach to prevent other instances of Excel opening the file, but you could still open the version on disk in a hex editor and change it, while Excel has it open. So there simply isn't any generic solution for this.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?