how to lock files

  • Thread starter Thread starter Piotrek Stachowicz
  • Start date Start date
P

Piotrek Stachowicz

Hi,
I'm testing my application, and I'd like to check how it behaves in
situations where access to the files it uses is somewhow not possible, for
instance the file is locked, and here goes my question. How can I lock the
file so that other program is not able to use it? Will opening it in some
text editor suffice?

Piotrek
 
Piotrek said:
Hi,
I'm testing my application, and I'd like to check how it behaves in
situations where access to the files it uses is somewhow not possible, for
instance the file is locked, and here goes my question. How can I lock the
file so that other program is not able to use it? Will opening it in some
text editor suffice?
Hi

Most text editors doesn't lock the file.

The VBScript below will lock files (OpenTextFile can be used
for non-text files as well in this scenario).


'--------------------8<----------------------

Const ForAppending = 8
Set oFSO = CreateObject("Scripting.FileSystemObject")

' open file(s) for appending to lock it/them
Set f = oFSO.OpenTextFile("C:\Tst\My1.exe", ForAppending, True)
Set f = oFSO.OpenTextFile("C:\Tst\My2.exe", ForAppending, True)

MsgBox "Press OK to unlock the file(s)"
'--------------------8<----------------------
 
Torgeir Bakken (MVP) said:
Hi

Most text editors doesn't lock the file.

The VBScript below will lock files (OpenTextFile can be used
for non-text files as well in this scenario).


'--------------------8<----------------------

Const ForAppending = 8
Set oFSO = CreateObject("Scripting.FileSystemObject")

' open file(s) for appending to lock it/them
Set f = oFSO.OpenTextFile("C:\Tst\My1.exe", ForAppending, True)
Set f = oFSO.OpenTextFile("C:\Tst\My2.exe", ForAppending, True)

MsgBox "Press OK to unlock the file(s)"
'--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx


Are both files supposed to remain locked until "OK" is pressed? Out of
curiousity, I just tested the sample script and only My2.exe seems to have
been locked.


Greg
 
GO said:
Are both files supposed to remain locked until "OK" is pressed?
Out of curiousity, I just tested the sample script and only
My2.exe seems to have been locked.
Hi

Sorry, there was a bug in the code, here is an updated version (2 x f
is changed to f1 and f2):


'--------------------8<----------------------

Const ForAppending = 8
Set oFSO = CreateObject("Scripting.FileSystemObject")

' open file(s) for appending to lock it/them
Set f1 = oFSO.OpenTextFile("C:\Tst\My1.exe", ForAppending, True)
Set f2 = oFSO.OpenTextFile("C:\Tst\My2.exe", ForAppending, True)

MsgBox "Press OK to unlock the file(s)"
'--------------------8<----------------------
 
Back
Top