代码之家  ›  专栏  ›  技术社区  ›  John D

如何在FTP列表目录中搜索下载特定文件的模式

  •  -1
  • John D  · 技术社区  · 6 年前

    我正在为我的GUI开发一个下载功能,该功能允许最终用户输入一个5位数的作业编号,并仅从FTP站点下载这些文件。在这样做的过程中,我已经能够获得目录列表,但我无法使用该列表来获取文件。如果您对显示的代码有任何帮助,我们将不胜感激。

        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
    
    2 回复  |  直到 2 年前
        1
  •  1
  •   John D    6 年前

    下面是有效的最终输出。总体

        Dim UserName As String
        ' Sets Username to current logged-in user profile
        UserName = Environment.UserName
    
        Dim JobNo As String
        JobNo = Textbox1.Text
    
        'DOWNLOADING FROM THE FTP JOBS PROCESSED FOLDER
        Dim listRequest As FtpWebRequest = WebRequest.Create(“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)
    
        'CREATES TEAM FILE FOLDERS ON LOCAL COMPUTER
        My.Computer.FileSystem.CreateDirectory("C:\users\" & UserName & "\desktop\TEAMFILES\IMB_APPEND\A_TEAM")
        My.Computer.FileSystem.CreateDirectory("C:\users\" & UserName & "\desktop\TEAMFILES\IMB_APPEND\B_TEAM")
    
        'CLEAR TEXTBOX2
        TextBox2.Clear()
    
        For Each foundFile As String In directory
            ATEAMdown(foundFile)
            TextBox2.Text = TextBox2.Text & foundFile & vbNewLine
        Next
    
        'DOWNLOADING FROM THE IMB FTP XMPIE TEAM FOLDER
        listRequest = WebRequest.Create(“ftp.site.com/input/” & JobNo & "_*.DBF")
        listRequest.Credentials = New System.Net.NetworkCredential(“Username”, “Password”)
        listRequest.Method = WebRequestMethods.Ftp.ListDirectory
        listResponse = listRequest.GetResponse()
        reader = New System.IO.StreamReader(listResponse.GetResponseStream())
        Filedata = reader.ReadToEnd
        directory = Filedata.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
    
        For Each foundFile As String In directory
            BTEAMdown(foundFile)
            TextBox2.Text = TextBox2.Text & foundFile & vbNewLine
        Next
    
        2
  •  0
  •   John D    6 年前

    我试图根据使用ListDirectory创建的列表从FTP下载文件,但它没有从读卡器中以可用格式拆分,因此无法使用。我已更新了代码并使其正常工作:

        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
    

    更新的部分

        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)