how to lock files

  • Thread starter Piotrek Stachowicz
  • 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
 
T

Torgeir Bakken \(MVP\)

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<----------------------
 
G

GO

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
 
T

Torgeir Bakken \(MVP\)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top