how to determine if file transfer is complete with vb6

Posted on 2006-05-01
Last Modified: 2010-04-07
I have a program that is looking in a specific folder for incoming zip files.  These files are coming through a 56k pipe on our network so it usually takes approximately 1-2 minutes for the file to be completely copied from the remote location.  I need a way to determine when the file is released(not in use/transfer complete) so I can then test to make sure it is a valid zip and then process accordingly.  If the connection breaks and the file doesn't get transferred completely, I then test to determine whether it is a valid zip or not and process accordingly.  

If I run the test before the file transfer completes, it just looks like it is a bad zip.  The problem is that sometimes there are broken transmissions and there are actually bad zips out there.  When I come across these files I move them to a "bad zip" folder.

Is there anything that writes to the file attribute or any sort of flag that signals when the file is released or the transfer is finished?
Question by:edfreels
    LVL 142

    Accepted Solution

    If you have the chance to control the sending application, ask it (ie the developer) to send a control file after sending the zip files
    the control file should have the same name as the zip file (except the .zip extension), it's pure presence telling the destination that the zip file transfer is complete.

    if that cannot be done, you should try to open the file with a exclusive lock, if that fails, the file is still being transferred.
    Open pathname For output lock As #1

    if that line gives an error, the file is not yet done.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Along the same lines as angelIIIs comments....if you can control the filenames being sent to you....I once embedded each file size into its filename so I could see if it was all there or not.
    LVL 1

    Author Comment


    It is not feasible to do your first suggestion but your second suggestion is intriguing. Can you explain what this line is doing?  

    I am receiving a compile error "Expected: Read or Write" with the "AS" highlighted in the line of code after adding.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    You need "Read" or "Write" after the "Lock":

        Open fileName For Output Lock Write As #1

    If you get an error (trapped with On Error statements) then another process has the file open, preventing you from getting the Lock.  In this case, the file is still being transferred...
    LVL 1

    Author Comment


    Thanks for you help.  You both got me on the right track but I found a function that did exactly what I needed using API calls.  Again, you pointed me in the right direction though so I will give you both credit.  Thanks!  Just to share for future knowledge, below is a function to check if a file is open.  With this I could just do an if...then and bypass the file if it was still being transferred and go on to the next file:

    ' Declaration for APIs used by IsFileAlreadyOpen function...
        Private Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
        Private Declare Function GetLastError Lib "kernel32" () As Long
        Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long

    ' Our Function...
    Function IsFileAlreadyOpen(Filename As String) As Boolean
       Dim hFile As Long
       Dim lastErr As Long

       ' Initialize file handle and error variable.
       hFile = -1
       lastErr = 0

       ' Open for for read and exclusive sharing.
       hFile = lopen(Filename, &H10)

       ' If we couldn't open the file, get the last error.
       If hFile = -1 Then
          lastErr = Err.LastDllError
       ' Make sure we close the file on success.
          lclose (hFile)
       End If

       ' Check for sharing violation error.
       If (hFile = -1) And (lastErr = 32) Then
          IsFileAlreadyOpen = True
          IsFileAlreadyOpen = False
       End If

    End Function

    This helped me determine whether the file transfer had completed.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction This article makes the case for using two modules in your VBA/VB6 applications to provide both case-sensitive and case-insensitive text comparison operations.  Recently, I solved an EE question using the LIKE function.  In order for th…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now