Test for open files

Ideas for new features & functions

Moderators: Dorian (MJT support), JRL

Post Reply
JoeBo747
Newbie
Posts: 10
Joined: Thu Sep 08, 2005 8:42 am

Test for open files

Post by JoeBo747 » Fri Sep 09, 2005 7:58 am

Is it possible to loop through a files and return an error if any are in an open state. :?:
JOE

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Fri Sep 09, 2005 11:29 am

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]

JoeBo747
Newbie
Posts: 10
Joined: Thu Sep 08, 2005 8:42 am

Post by JoeBo747 » Fri Sep 09, 2005 11:51 am

Thanks for the responce. I have looked at sample code for VBScript file system object, but cannot find a reference to the open status of the file. Could you give me a pointer. :?:
regards
JOE

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Fri Sep 09, 2005 12:09 pm

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
MJT Net Support
[email protected]

JoeBo747
Newbie
Posts: 10
Joined: Thu Sep 08, 2005 8:42 am

Post by JoeBo747 » Fri Sep 09, 2005 2:35 pm

I don’t know if I am wrong in what I am trying to achieve> I have adapted your code as follows, if I try to zip the file logon.rnd I get a complaint that the file is in use when I test the file with the code it returns “Not Lockedâ€Â
JOE

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

tested and does not seem to work

Post by kpassaur » Thu Mar 16, 2006 1:29 am

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?

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Mar 16, 2006 8:40 am

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?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts