简体   繁体   English

从FTP下载文件(如果给定的本地文件列表中不存在)

[英]Download file from FTP if it does not exist in a given list of local files

I have an FTP server from which I want to download all the files that do not exist in my local directory. 我有一个FTP服务器,我想从中下载本地目录中不存在的所有文件。

I tried doing a For Next but I just can't get my head around it. 我尝试做一个For Next但是我无法理解。 I tried enumerating the files but as a result of doing it to both lists, I got an error. 我尝试枚举文件,但是由于对两个列表都进行了处理,所以出现了错误。 I think the error might be caused from cross checking the online files with an individual enumerated file from the local list. 我认为该错误可能是由交叉检查在线文件与本地列表中的单个枚举文件引起的。 How do I eliminate this problem? 如何消除这个问题?

Link to FTPClient Class Code: 链接到FTPClient类代码:

https://docs.google.com/file/d/0BxFwEuHe1g77TEw2ckZxVUlQdGM/edit?usp=sharing https://docs.google.com/file/d/0BxFwEuHe1g77TEw2ckZxVUlQdGM/edit?usp=sharing

All Code: 所有代码:

          Dim ftp As New FTPclient("ftp://www.ahpg.zxq.net", "eg", "eg")

    Dim dirList As FTPdirectory = ftp.ListDirectoryDetail("/")
    Dim result As List(Of String) = ftp.ListDirectory("/")
    For Each line As String In result
        FTPLBX.Items.Add(line)
    Next
    Dim str As String
    Dim locstr As String
    Dim res_numer As IEnumerator
    res_numer = result.GetEnumerator()
    Dim loclist As List(Of String) = New List(Of String) _
                                     (System.IO.Directory.EnumerateFiles("C:/Program Files/Business Elements/Recent Files"))
    Dim LOC_Enum As IEnumerator
    LOC_Enum = loclist.GetEnumerator
    Do While LOC_Enum.MoveNext
        locstr = (LOC_Enum.Current)
    Loop
    Do While (res_numer.MoveNext)
        str = (res_numer.Current)
    Loop

    For Each str In loclist
        If Not loclist.Contains(str) = True Then
            My.Computer.Network.DownloadFile("ftp://www.ahpg.zxq.net/ftpfiles/" & str.ToString, _
                                             "C:/Program Files/Business Elements/Recent Files/" & str.ToString, "eg", "eg")
            MessageBox.Show("Done ")
        End If
    Next

End Sub

I made it a little easier if it works for you. 如果适合您,我将它简化了一点。 Here you go: 干得好:

    ' Your instance of FTPClient
    Dim ftp As New FTPclient("ftp://www.ahpg.zxq.net", "eg", "eg")

    ' The path to destination folder (Local directory)
    Dim localDir As String = "C:/Program Files/Business Elements/Recent Files/"

    ' Lists all the file in the given directory of FTP server
    For Each file As FTPfileInfo In ftp.ListDirectoryDetail("/").GetFiles

        Try
            ftp.Download(file, localDir & file.Filename)

            ' The FTPClient class throws exception if the 
            ' file already exists in destination directory
        Catch e As ApplicationException
            Console.WriteLine(e.Message)
        End Try
    Next file

Note 1 : I downloaded the FTPClient class from CodeProject but it's almost the same as the one you provided in the question. 注意1 :我从CodeProject下载了FTPClient类,但它与您在问题中提供的类几乎相同。

Note 2 : The FTPClient itself throws an exception if the file exists in your destination folder. 注意2 :如果文件在目标文件夹中,则FTPClient本身会引发异常。 So you don't need to bother comparing the files. 因此,您无需费心比较文件。

Note 3 : Notice the trailing slash at the end of locadDir string. 注3 :请注意locadDir字符串末尾的斜杠 Without that, the file will be downloaded to the Business Element folder. 否则,文件将被下载到Business Element文件夹。

Write a sub-method, let's call it IsExistedInLocal to check if the file from ftp is already in your local or not. 编写一个子方法,我们称其为IsExistedInLocal,以检查ftp中的文件是否已在您的本地文件中。 If it is, ignore and move on to the next one; 如果是,请忽略并继续进行下一个; if it is not not, download the file.I assume you know all the detail so pseudo code should be okay 如果不是,请下载文件。我假设您知道所有详细信息,因此伪代码应该可以

for each FTP_file in FTP_FilesList
  if not IsExistedInLocal(FTP_file) then
    download the file to local
  end if
next

In the for loop you are taking each entry in the list and then checking that same list doesn't have that element. 在for循环中,您将获取列表中的每个条目,然后检查同一列表中没有该元素。 Your if condition will never be true and never reach the DownloadFile statement. 您的if条件永远不会为真,也永远不会到达DownloadFile语句。

For Each str In loclist
    If Not loclist.Contains(str) = True Then
        My.Computer.Network.DownloadFile("ftp://www.ahpg.zxq.net/ftpfiles/" & str.ToString, _
                                         "C:/Program Files/Business Elements/Recent Files/" & str.ToString, "eg", "eg")
        MessageBox.Show("Done ")
    End If
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM