Link to home
Start Free TrialLog in
Avatar of andyd273
andyd273

asked on

FTP API Problems

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.
There are problems where it fails on the FtpGetFile even when the file exists and the connection works.
It also seems to pause or freeze sometimes in the middle of a loop.

Private hConnection As Long
Private hOpen As Long

Function amdDownloadStart()
    Dim WFD As WIN32_FIND_DATA
    Dim sPath As String
    Dim hFind As Long
    Dim sFileSize As String
    Dim tmp As String
    Dim isTesting As String
   
    Dim Temp() As String
    Dim FileName As String
    Dim sOrgPath As String
    Dim hFindConnect As Long
    Dim itmX As ListItem
    Dim rSelect As New Recordset
   
    'open an internet connection
    hOpen = InternetOpen("Download", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)

    'connect to the FTP server
    hConnection = InternetConnect(hOpen, FTPSite, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)

    StatusBar.Panels(1).Text = "Connected"
    DoEvents
   
    With rSelect
        .Open "Select * From tblScanusers Where Not TracksID = 0", myApp.MyCn, adOpenForwardOnly, adLockReadOnly
        Do While Not .EOF

            StatusBar.Panels(1).Text = "Connected. Checking: " & !UserName
            DownloadFiles !TracksID, !UserName, "new"
            DoEvents
            DownloadFiles !TracksID, !UserName, "corrected"
            DoEvents
            .MoveNext
        Loop
        .Close
    End With
   
    StatusBar.Panels(1).Text = "Done"
    DoEvents
   
    'close the internet connection
    InternetCloseHandle hConnection
   
    InternetCloseHandle hOpen
   
    StatusBar.Panels(1).Text = "Disconnected. T-5"
    DoEvents
   
End Function

Function DownloadFiles(CustomerID As Long, UserName As String, Path As String)
    Dim WFD As WIN32_FIND_DATA
    Dim sPath As String
    Dim hFind As Long
    Dim sFileSize As String
    Dim tmp As String
   
    Dim Temp() As String
    Dim FileName As String
    Dim sOrgPath As String  'hConnection As Long, hOpen As Long,
    Dim hFindConnect As Long
    Dim itmX As ListItem
    Dim rSelect As New Recordset
    Dim fileget As String
    Dim LocalName As String
    Dim LocalDirectory As String
    Dim fFlag As Boolean
    fFlag = False
   
    LocalName = localUserName
   
    hFindConnect = InternetConnect(hOpen, FTPSite, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, INTERNET_FLAG_EXISTING_CONNECT Or INTERNET_FLAG_PASSIVE, &H0)

    'create a buffer to store the original directory
    sOrgPath = String(MAX_PATH, 0)
   
    'get the directory
    FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
   
    LocalDirectory = "C:\Documents and Settings\" & LocalName & "\Desktop\Incoming\" & UserName & "\" & Path & "\"
   
    'set connection for getting the file names
    If hFindConnect Then
        sPath = "public/dealers/" & UserName & "/" & Path & "/" '"/public/dealers/andyd273/new"
        FtpSetCurrentDirectory hConnection, "public/dealers/" & UserName & "/" & Path & "/"
        DoEvents
        hFind = FtpFindFirstFile(hFindConnect, sPath, WFD, INTERNET_FLAG_RELOAD Or INTERNET_FLAG_NO_CACHE_WRITE, 0&)
        If hFind Then
            Do
                tmp = StripNull(WFD.cFileName)
               
               DoEvents
               If Len(tmp) Then
                    If WFD.dwFileAttributes And Not vbDirectory Then
               
                        If Not DirExists(LocalDirectory) Then '
                            NewDirectory LocalDirectory
                        End If
                            DoEvents
                       
                        fileget = ""
                        Do
                            StatusBar.Panels(1).Text = "Downloading File " & tmp
                            DoEvents
                            fileget = FtpGetFile(hConnection, tmp, LocalDirectory & tmp, True, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0)
                            DoEvents
                        Loop While fileget = ""
                        If fileget = "True" Then
                            DoEvents
                            amdSaveFile CustomerID, LocalDirectory & tmp, Path, False
                            ListView1.ListItems.Add , LocalDirectory & tmp, LocalDirectory & tmp
                            DoEvents
                            FtpDeleteFile hConnection, tmp
                            StatusBar.Panels(1).Text = "Download Finished"
                            DoEvents
                        Else
                            StatusBar.Panels(1).Text = "Download Failed"
                            MsgBox "Download Failed: " & LocalDirectory & tmp
                        End If
                    End If
                End If
                DoEvents
            Loop While InternetFindNextFile(hFind, WFD)
        End If
    End If

    InternetCloseHandle hFind
    InternetCloseHandle hFindConnect
   
    hFindConnect = 0
    hFind = 0

End Function
Avatar of tonym001
tonym001

Try changing the INTERNET_FLAG_PASSIVE value in the InternetOpen function from false to true or vise-versa
Avatar of andyd273

ASKER

I'm really not sure how to do this...

it gets set with:
Const INTERNET_FLAG_PASSIVE = &H8000000                    ' used for FTP connections

to turn it off would I just set it to 0?
Sorry was not very clear there. What I should have said was this
You need a boolean whose value you set before calling the InternetConnect function .
For instance
Dim blnPassive as boolean
'set the value
blnPassive = true
hConnection = InternetConnect(hOpen, FTPSite, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, IIf(blnPassive, INTERNET_FLAG_PASSIVE, _
      0), 0)

Changing the blnPassive value may be the solution as I have encountered a similar problem but it is just guesswork on my part. Hope it works

Sorry it took me a few days to check this...
I've tried it several ways and it still failed, but it seemed to help some.
Then I put a do while loop with 20000 iterations

                                Do While Not hConnection And Count < 20000
                                    DoEvents
                                    Count = Count + 1
                                Loop

which seems to help more.

But now I'm running into another problem, which is that it seems that the connections aren't terminating.
I downloaded some files, and then tried going in through a normal ftp program to upload some more, and got a message that there were to many connections (15).

I have some stuff to close the connections
    InternetCloseHandle hFind
    InternetCloseHandle hFindConnect
    InternetCloseHandle hConnection
    InternetCloseHandle hOpen
but is this the best way to do it?
ASKER CERTIFIED SOLUTION
Avatar of tonym001
tonym001

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial