[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


how to determine if file transfer is complete with vb6

Posted on 2006-05-01
Medium Priority
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
  • 2
  • 2
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 1050 total points
ID: 16580534
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 86

Expert Comment

by:Mike Tomlinson
ID: 16580914
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.

Author Comment

ID: 16581161

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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 450 total points
ID: 16581535
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...

Author Comment

ID: 16582548

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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 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