Results 1 to 1 of 1

Thread: FTP Downloading

Hybrid View

  1. #1
    New Member
    Join Date
    Jul 07
    Posts
    13

    FTP Downloading

    I have a VB6 program that is supposed to search through a bunch of FTP directories and download any files that it finds in them.
    It searches just fine, and locates any directories that have files.
    The problem is when it fails on the FtpGetFile, even when the file exists and the connection works.
    Sometimes it will fail on the first file in the directory and get the rest, it could be that I just don't understand how the InternetConnect stuff works.
    It also seems to pause or freeze sometimes in the middle of a loop.
    What do I need to do to make the FTP API more dependable?

    vb Code:
    1. Function amdDownloadStart()
    2.     Dim WFD As WIN32_FIND_DATA
    3.     Dim sPath As String
    4.     Dim hFind As Long
    5.     Dim sFileSize As String
    6.     Dim tmp As String
    7.     Dim isTesting As String
    8.    
    9.     Dim Temp() As String
    10.     Dim FileName As String
    11.     Dim sOrgPath As String
    12.     Dim hFindConnect As Long
    13.     Dim itmX As ListItem
    14.     Dim rSelect As New Recordset
    15.    
    16.     StatusBar.Panels(1).Text = "Connected"
    17.     DoEvents
    18.    
    19.     If chkTesting Then
    20.         isTesting = " And type = 'ALL'"
    21.     Else
    22.         isTesting = " And Not (type = 'ALL' Or type='JL')"
    23.     End If
    24.    
    25.     With rSelect
    26.         .Open "Select * From tblScanusers Where Not TracksID = 0" & isTesting, myApp.MyCn, adOpenForwardOnly, adLockReadOnly
    27.         Do While Not .EOF
    28.  
    29.             StatusBar.Panels(1).Text = "Connected. Checking: " & !UserName
    30.             DownloadFiles !TracksID, !UserName, "new"
    31.             DoEvents
    32.             DownloadFiles !TracksID, !UserName, "corrected"
    33.             DoEvents
    34.             .MoveNext
    35.         Loop
    36.         .Close
    37.     End With
    38.    
    39.     StatusBar.Panels(1).Text = "Done"
    40.     DoEvents
    41.    
    42.     'close the internet connection
    43.     InternetCloseHandle hConnection
    44.    
    45.     InternetCloseHandle hOpen
    46.    
    47.     StatusBar.Panels(1).Text = "Disconnected. T-5"
    48.     DoEvents
    49.    
    50. End Function
    51.  
    52. Function DownloadFiles(CustomerID As Long, UserName As String, Path As String)
    53.     Dim WFD As WIN32_FIND_DATA
    54.     Dim sPath As String
    55.     Dim hFind As Long
    56.     Dim sFileSize As String
    57.     Dim tmp As String
    58.    
    59.     Dim Temp() As String
    60.     Dim FileName As String
    61.     Dim sOrgPath As String, hConnection As Long, hOpen As Long, hFindConnect As Long
    62.     Dim itmX As ListItem
    63.     Dim rSelect As New Recordset
    64.     Dim fileget As String
    65.     Dim LocalName As String, LocalDirectory As String
    66.     Dim fFlag As Boolean
    67.     fFlag = False
    68.    
    69.     LocalName = localUserName
    70.    
    71.     'open an internet connection
    72.     hOpen = InternetOpen("Download", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    73.    
    74.     'connect to the FTP server
    75.     hConnection = InternetConnect(hOpen, FTPSite, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    76.    
    77.     hFindConnect = InternetConnect(hOpen, FTPSite, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, INTERNET_FLAG_EXISTING_CONNECT Or INTERNET_FLAG_PASSIVE, &H0)
    78.  
    79.     'create a buffer to store the original directory
    80.     sOrgPath = String(MAX_PATH, 0)
    81.    
    82.     'get the directory
    83.     FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    84.    
    85.     LocalDirectory = "C:\Documents and Settings\" & LocalName & "\Desktop\Incoming\" & UserName & "\" & Path & "\"
    86.    
    87.     'set connection for getting the file names
    88.     If hFindConnect Then
    89.         sPath = "public/dealers/" & UserName & "/" & Path & "/" '"/public/dealers/andyd273/new"
    90.         FtpSetCurrentDirectory hConnection, "public/dealers/" & UserName & "/" & Path & "/"
    91.         DoEvents
    92.         hFind = FtpFindFirstFile(hFindConnect, sPath, WFD, INTERNET_FLAG_RELOAD Or INTERNET_FLAG_NO_CACHE_WRITE, 0&)
    93.         If hFind Then
    94.             Do
    95.                 tmp = StripNull(WFD.cFileName)
    96.                
    97.                DoEvents
    98.                If Len(tmp) Then
    99.                     If WFD.dwFileAttributes And Not vbDirectory Then
    100.                
    101.                         If Not DirExists(LocalDirectory) Then '
    102.                             NewDirectory LocalDirectory
    103.                         End If
    104.                             DoEvents
    105.                        
    106.                         fileget = ""
    107.                         Do
    108.                             StatusBar.Panels(1).Text = "Downloading File " & tmp
    109.                             DoEvents
    110.                             fileget = FtpGetFile(hConnection, tmp, LocalDirectory & tmp, True, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0)
    111.                             DoEvents
    112.                         Loop While fileget = ""
    113.                         If fileget = "True" Then
    114.                             DoEvents
    115.                             amdSaveFile CustomerID, LocalDirectory & tmp, Path, False
    116.                             ListView1.ListItems.Add , LocalDirectory & tmp, LocalDirectory & tmp
    117.                             DoEvents
    118.                             FtpDeleteFile hConnection, tmp
    119.                             StatusBar.Panels(1).Text = "Download Finished"
    120.                             DoEvents
    121.                         Else
    122.                             StatusBar.Panels(1).Text = "Download Failed"
    123.                             MsgBox "Download Failed: " & LocalDirectory & tmp
    124.                         End If
    125.                     End If
    126.                 End If
    127.                 DoEvents
    128.             Loop While InternetFindNextFile(hFind, WFD)
    129.         End If
    130.     End If
    131.  
    132.     InternetCloseHandle hFind
    133.     InternetCloseHandle hFindConnect
    134.     InternetCloseHandle hConnection
    135.     InternetCloseHandle hOpen
    136.    
    137.     hFindConnect = 0
    138.     hFind = 0
    139.  
    140. End Function
    Last edited by andyd273; Jul 26th, 2007 at 10:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •