Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VBScript Checking if file is open

Options
  • 01-11-2007 7:12pm
    #1
    Registered Users Posts: 227 ✭✭


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


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭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.


  • Closed Accounts Posts: 345 ✭✭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


  • Registered Users Posts: 227 ✭✭rebellad


    Thanks guys, will give this a go in the morning when I get back to work


Advertisement