yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: How do I know if a text file is open and being written on?
Hello. Regardless of the file type (some types, like .mdb, create a temporary file which you can search for), if you attempt to use a Name instruction on a file to rename it, you get a 75 error if the file is in use, no matter by what process.
So you can easily build a function to do the test, trying to rename the file: if you succeed, the file is not open.
Some kind of stuff like following: Public Function FileInUse(FilePath As String) As Boolean
On Error GoTo ErrHandler
Dim NewName As String
' assumes that the path actually points to an existant file
NewName = FilePath & "***"
' try to rename the file
Name FilePath As NewName
' if the code reaches this point, the file is not in use
' so we have to restore the previous name before exit
Name NewName As FilePath
ExitPoint:
Exit Function
ErrHandler:
If Err.Number = 75
FileInUse = True
End If
Resume ExitPoint
End Function | It doesn't manage cases like mistyped filenames or dir names, or else, but you can improve this technique by your own.
It's a trick: I found it by myself, and I use it when I need. But it's not documented, and the Name function is not easy to reach in the Help (if you type Name and press F1, you get addressed to the Name property)
Hope it helps.
____________________________
Real Programmer can count up to 1024 on his fingers
|