how to determine if file transfer is complete with vb6

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?
LVL 1
edfreelsAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
edfreelsAuthor Commented:
AngelIII,

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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0
 
edfreelsAuthor Commented:
Guys,

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.
   Else
      lclose (hFile)
   End If

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

End Function


This helped me determine whether the file transfer had completed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.