views:

2344

answers:

4

Need help with this one.

I have a VBS script that currently writes to a logfile. This script can be kicked off my multiple simultaneous processes so now I'm worried about concurreny.

I'm currently using the FilesystemObject to open and write to this file. Does FSO support exclusive file access?

Thanks in Advance

+1  A: 

Yes, FileSystemObject does support exclusive file access. If another process has a lock on the file when you call OpenTextFile, you will get an error (a permission denied error). You should be able to trap the error and handle it appropriately (check that Err.Number <> 0 after the call to OpenTextFile is one way you could do that).

Jason Down
A: 

I don't know how accessable the Windows API is to you, but you should have a look at the Debugging infrastructure in the Windows API.

Theres a good Code Project article on it here Basically using OutputDebugString and catching that with DebugView, or piping it to file will remove you issue with locking the log file, also will remove the bottle neck of multiple scripts queued to write to the log file.

Hope this helps.

Binary Worrier
A: 

Using Err.Number <> 0 worked great.

THanks a million again!

A: 

If at all possible I recommend you close the handle and reopen it every time you need to write to the file, to avoid possible exclusive file access issues. Ex:

set objFile = objFSO.OpenTextFile("somefile.txt",8,True)
objFSO.WriteLine "jfdskfdkls"
objFSO.Close
'something something
set objFile = objFSO.OpenTextFile("somefile.txt",8,True)
objFSO.WriteLine "gfdgfdgfd"
objFSO.Close
'something else
set objFile = objFSO.OpenTextFile("somefile.txt",8,True)
objFSO.WriteLine "ddsgfgdfsgdfs"
objFSO.Close
mrTomahawk