'How to search an FTP ListDirectory for a pattern to download specific files

I am working on a download feature for my GUI that will allow the end user to be able to input a 5 digit job number and download only those files from the FTP site. In doing this, I have been able to get a list of the directory, but I have not been able to use that list to get the files. Any help on the code shown would be appreciated.

    Dim UserName As String
    ' Sets Username to current logged-in user profile
    UserName = Environment.UserName


    Dim JobNo As String
    JobNo = Textbox1.Text

    Dim listRequest As FtpWebRequest = WebRequest.Create("ftp://ftp.site.com/INPUT/" & JobNo & "_*.DBF")
    listRequest.Credentials = New System.Net.NetworkCredential(“Username”, “Password”)
    listRequest.Method = WebRequestMethods.Ftp.ListDirectory
    Dim listResponse As FtpWebResponse = listRequest.GetResponse()
    Dim reader As StreamReader = New StreamReader(listResponse.GetResponseStream())



    For Each foundFile As String In
        My.Computer.Network.DownloadFile("ftp://ftp.site.com/INPUT/" & foundFile, "C:\users\” & UserName & “\desktop\temp\" & foundFile, “Username”, “Password”)
    Next


Solution 1:[1]

I was trying to download files from the FTP based on the list that was created using ListDirectory, but it was not split in a usable format from the reader and therefore could not be used I have updated my code and have it working:

    Dim UserName As String
    ' Sets Username to current logged-in user profile
    UserName = Environment.UserName


    Dim JobNo As String
    JobNo = Textbox1.Text

    Dim listRequest As FtpWebRequest = WebRequest.Create("ftp://ftp.site.com/INPUT/" & JobNo & "_*.DBF")
    listRequest.Credentials = New System.Net.NetworkCredential(“Username”, “Password”)
    listRequest.Method = WebRequestMethods.Ftp.ListDirectory
    Dim listResponse As FtpWebResponse = listRequest.GetResponse()

    Dim reader As StreamReader = New System.IO.StreamReader(listResponse.GetResponseStream())
    Dim Filedata As String = reader.ReadToEnd
    Dim directory() As String = Filedata.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)

    For Each foundFile As String In directory
        My.Computer.Network.DownloadFile("ftp://ftp.site.com/INPUT/" & foundFile, "C:\users\” & UserName & “\desktop\temp\" & foundFile, “Username”, “Password”)
    Next

Updated Section

    Dim reader As StreamReader = New System.IO.StreamReader(listResponse.GetResponseStream())
    Dim Filedata As String = reader.ReadToEnd
    Dim directory() As String = Filedata.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John D