• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1194
  • Last Modified:

FTP retrieving a file using Visual Basic FTPGetFile and wildcards

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.
  • 3
1 Solution
Nathan RileyFounder/CTOCommented:
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

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.

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("ftp.site.com", 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

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

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.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now