Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do not allow access to files being processed 1

Status
Not open for further replies.

mmetze

Programmer
Oct 2, 2001
45
US
What is the best way to check to see if a file is being accessed by another process? Specifically, if the file is being written, I want to restrict access in a For Each loop when using the FileSystemObject in the Scripting library. For example, I am looking for the code required in the 'FileIsLocked' function in the sample code below...

For Each objFile in objFolder
If Not FileIsLocked(objFile) Then
ProcessFile(objFile)
End If
Next objFile
 
this might help you, might not...

Option Explicit

Sub main()
Dim oFile As File
Dim oFSO As FileSystemObject
Dim oFolder As Folder
Dim oStream As TextStream

Set oFSO = New FileSystemObject
Set oFolder = oFSO.GetFolder("C:\")
Set oStream = oFSO.OpenTextFile("C:\TinyInvaders.exe", ForAppending)

Set oFSO = Nothing

For Each oFile In oFolder.Files
If FileIsLocked(oFile) Then
MsgBox oFile.Name & " is locked"
Else
Debug.Print oFile.Name & " is not locked"
End If
DoEvents
Next oFile

Set oFile = Nothing
Set oFolder = Nothing

oStream.Close
Set oStream = Nothing
End Sub

Private Function FileIsLocked(ByVal oFile As File) As Boolean
Dim oStream As TextStream

On Error GoTo ERRORHANDLER

Set oStream = oFile.OpenAsTextStream(ForAppending)
On Error GoTo 0

Set oFile = Nothing

oStream.Close
Set oStream = Nothing
FileIsLocked = False
Exit Function

ERRORHANDLER:
Debug.Print ">>>>", Err.Description, oFile.Name
FileIsLocked = True
Set oFile = Nothing
Set oStream = Nothing
End Function
 
This seems to work well!!! I just have a few questions...

1- The "On Error GoTo 0" stmt is meant to re-execute the entire function upon a failure in closing the textstream object and destroying the fso objects? I'm not trying to be a jerk by asking this, I just want to make sure I'm not missing something. :)

2- I assume that opening the file as a textstream is more efficient than opening as a random access file using the 'open' method? Any drawbacks in this situation?

3- In testing this, I opened the desired file in WordPad and then stepped through the code without an error being raised. However, when I opened the file with MS-Word, an error was raised. Could it be that Word locks a file when opening, but WordPad does not?


Again, much thanks for the code! I really appreciate the help.

Mark
 
I put the 'on error goto 0' statement where it was as i only wanted to catch the error when trying to open the file and not all possible errors in the code

I used open as text stream because you were using the filesystemobject and had assumed that you were too

i myself prefer the regular open statement as I can then explicitly open a file locked for reading, writing or both if I wanted to
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top