FTP retrieving a file using Visual Basic FTPGetFile and wildcards

Posted on 2008-11-07
Last Modified: 2012-06-27
I am writing a VB6 module that retrieves a file from an FTP server using the FtpGetFile command.  Is there a way to use wildcards with the FtpGetFile command when trying to retrieve a file?  For example, the file on the ftp server is "myfile_2008-11-06.dat".  I want to retrieve the file using a wildcard in the filename expression like "myfile*.dat".   The FtpGetFile command doesn't appear to like or recognize the "*" as a wildcard.
Question by:VBStudent
    LVL 11

    Expert Comment

    by:N R
    Not exactly sure how as someone had created it before me.  But it has something to do with a global variable.  
    localInvoiceFile = "C:\Clients\Strat\File Uploads\Customer Uploads\ProMed\Working\" + Trim(DTSGlobalVariables("RemoteInvoiceFileName").Value)

    Open in new window

    LVL 29

    Expert Comment

    You won't be able to get that API to do that without some help. What you can do is use FtpFindFirstFile , FtpFindNextFile and then setup your own wildcard function.
    LVL 29

    Expert Comment


    LVL 29

    Accepted Solution

    The following should help you. Only uses a simple Like operation for the wildcard matching but you get the idea.
    Option Explicit
    Private Type WIN32_FIND_DATA_W
        dwFileAttributes            As Long
        ftCreationTime              As Currency
        ftLastAccessTime            As Currency
        ftLastWriteTime             As Currency
        nFileSizeHigh               As Long
        nFileSizeLow                As Long
        dwReserved0                 As Long
        dwReserved1                 As Long
        cFileName(520)              As Byte
        cAlternateFileName(28)      As Byte
    End Type
    Private Declare Function InternetOpenA Lib "wininet.dll" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    Private Declare Function InternetConnectA Lib "wininet.dll" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Long, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lcontext As Long) As Long
    Private Declare Function FtpFindFirstFileW Lib "wininet.dll" (ByVal hConnect As Long, ByVal lpszSearchFile As Long, ByVal lpFindFileData As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
    Private Declare Function InternetFindNextFileW Lib "wininet.dll" (ByVal hConnect As Long, ByVal lpvFindData As Long) As Long
    Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Long
    Private Declare Sub RtlZeroMemory Lib "kernel32" (ByRef dst As Any, ByVal nBytes As Long)
    Private Function ToString(ByVal szData As String) As String
        ToString = Left$(szData, InStr(1, szData, vbNullChar) - 1)
    End Function
    Public Sub EnumFtpDirectory( _
        ByVal szHost As String, _
        ByVal dwPort As Long, _
        ByVal szUser As String, _
        ByVal szPass As String, _
        ByVal szDirectory As String, _
        Optional ByVal szWildCard As String = vbNullString)
        '   Call EnumFtpDirectory("", 21, "username", "password", "/directory", "wildcard*")
        Dim hOpen       As Long
        Dim hConn       As Long
        Dim hFind       As Long
        Dim w32         As WIN32_FIND_DATA_W
        Dim szFileName  As String
        RtlZeroMemory w32, Len(w32)
        hOpen = InternetOpenA("FTP", 1, vbNullString, vbNullString, 1)
        hConn = InternetConnectA(hOpen, szHost, dwPort, szUser, szPass, 1, 0, 2)
        hFind = FtpFindFirstFileW(hConn, StrPtr(szDirectory), VarPtr(w32), 0, 0)
            szFileName = ToString(w32.cFileName)
            If LenB(szWildCard) = 0 Then
                ' * return all results. (ftpgetfile)...
                Debug.Print szFileName
                If szFileName Like szWildCard Then
                ' * return only wildcard results (ftpgetfile)...
                    Debug.Print szFileName
                End If
            End If
        Loop Until InternetFindNextFileW(hFind, VarPtr(w32)) = 0
        InternetCloseHandle hConn
        InternetCloseHandle hOpen
        InternetCloseHandle hFind
    End Sub

    Open in new window


    Author Closing Comment

    Thank you.....there is always more than one way to skin a cat.  I never thought of that way...

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now