• Jeff Moden (9/17/2014)


    P Jones (9/17/2014)


    ...and wait until it is fully available ....

    I'm curious. What are you using to determine that a file is done being written to and is ready for consumption? (other than a popup thermometer or fork :-P)

    Stick a finger in the air and see which way the wind's blowing. 😀

    Actualy a bit of vb script in a script task as the ftp comining in takes about 20 minutes to transfer a big zip file and the file shows as existing from the start but is locked by the ftp until fully done. I probably found the idea somewhere by Googling but it was a couple or three years ago. And I do lots of package logging so I can just run a quick query as a daily check to see package errors.

    While (FileLocked)

    Try

    ' Check if the file isn't locked by an other process by opening

    ' the file. If it succeeds, set variable to false and close stream

    fs = New FileStream(fnvar, FileMode.Open)

    ' No error so it is not locked

    logmsg = "File not locked: " & fnvar

    Dts.Log(logmsg, 0, b)

    FileLocked = False

    ' Close the file and exit the Script Task

    fs.Close()

    Dts.TaskResult = ScriptResults.Success

    Catch ex As IOException

    ' If opening fails, it's probably locked by an other process. This is the exact message:

    ' System.IO.IOException: The process cannot access the file 'D:\example.csv' because it is being used by another process.

    ' Log the locked status (once)

    If (ShowLockWarning) Then

    logmsg = "File locked: " & fnvar & " " & ex.Message

    Dts.Log(logmsg, 0, b)

    End If

    ShowLockWarning = False

    ' Wait two seconds before rechecking unless we've done 30 mins

    If DateDiff(DateInterval.Minute, Date.Now, starttime) < 30 Then

    Thread.Sleep(2000)

    Else

    logmsg = "Given up after waiting 30 mins. File locked: " & fnvar & " " & ex.Message

    Dts.Log(logmsg, 0, b)

    Dts.TaskResult = ScriptResults.Failure

    Exit While

    End If

    Catch ex As Exception

    ' Catch other unexpected errors and break the while loop

    logmsg = "Unexpected error: " & fnvar & " " & ex.Message

    Dts.Log(logmsg, 0, b)

    Dts.TaskResult = ScriptResults.Failure

    Exit While

    End Try

    End While