Discussions
Categories
Groups
Advertisement
Child Item
Home
Topics
Technology & Internet
Software & Web Development
Development
VBScript Checking if file is open
rebellad
I am polling a directory for a certain file and when it arrives I want to move it somewhere else via a vbscript. The problem at the moment is the file is getting moved even if it is only half written, anyone know of a way of checking if the file is being written to or is open??
Find more posts tagged with
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Best Of
Advertisement
Advertisement
Comments
seamus
Interesting that it's moving a file with an open handle. Perhaps attempt to open the file for writing as a "check" to see if it's still open. You should get an error thrown if the file is already open and VBScript attempts to open it. If you get a valid handle back, close the handle and move the file.
Although, for text files this may not always apply. Text files often never have locks put on them so anyone can read/write to them at the same time.
If this is on a network folder and is being written to from a network machine, you may be able to use WMI to determine if there's an open handle to the file.
FindingNemo
http://support.microsoft.com/kb/209189
If Not FileLocked(strFileName) then
etc etc etc
End If
Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
' Display the error number and description.
MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
FileLocked = True
Err.Clear
End If
End Function
rebellad
Thanks guys, will give this a go in the morning when I get back to work