Results 1 to 2 of 2

Thread: Search & Retrieve Matching files from FTP using wininet.dll api

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    21

    Search & Retrieve Matching files from FTP using wininet.dll api

    Hi all,

    I've been trying to use the wininet.dll api to search an ftp site for matching files but it's not working for some reason.
    Here's the method i've been using.


    Code:
    Private Sub DoStuff()
         Dim hConnection As Long, hOpen As Long, sOrgPath As String, lRes As Long
         Dim scUserAgent$
         
        scUserAgent$ = "vb wininet"
        hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
        hConnection = InternetConnect(hOpen, mServer$, INTERNET_DEFAULT_FTP_PORT, mUserid$, mPassword$, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)
        
        'set the current directory to 'root/testdir/testdir2'
        FtpSetCurrentDirectory hConnection, "testdir/testdir2"
    
        ReDim matchingFiles$(1)
        Call SearchForFiles(hConnection, ".txt", matchingFiles$)
        
        
        'Close the connections
        InternetCloseHandle hConnection
        InternetCloseHandle hOpen
    End Sub

    Code:
    Public Sub SearchForFiles(hConnection As Long, fileExtension$, matchingFiles$())
        Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
        Dim i%
        
        ReDim matchingFiles$(1)
        
        i% = 1
        
        'create a buffer
        pData.cFileName = String(MAX_PATH, 0)
        'find the first file
        hFind = FtpFindFirstFile(hConnection, "*." + fileExtension$, pData, 0, 0)
        
        'if there's no file, then exit sub
        
        If hFind = 0 Then Exit Sub
        'show the filename
        
        matchingFiles$(i%) = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
        
        Do
            i% = i% + 1
            'create a buffer
            pData.cFileName = String(MAX_PATH, 0)
            'find the next file
            lRet = InternetFindNextFile(hFind, pData)
            'if there's no next file, exit do
            If lRet = 0 Then Exit Do
            'show the filename
            ReDim Preserve matchingFiles$(UBound(matchingFiles) + 1)
            matchingFiles$(i%) = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
            
        Loop
        'close the search handle
        InternetCloseHandle hFind
    End Sub
    All i keep getting is "." and ".." for the files being returned from the SearchForFiles function. Am i doing something incorrectly?

    Thanks!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Search & Retrieve Matching files from FTP using wininet.dll api

    i tested your code, you are searching for "*..txt", other than that it worked fine, i did not get the . and .. returned, but that may depend on the type of ftp server
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width