简体   繁体   中英

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.

I tried doing a For Next but I just can't get my head around it. 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:

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.

Note 2 : The FTPClient itself throws an exception if the file exists in your destination folder. So you don't need to bother comparing the files.

Note 3 : Notice the trailing slash at the end of locadDir string. Without that, the file will be downloaded to the Business Element folder.

Write a sub-method, let's call it IsExistedInLocal to check if the file from ftp is already in your local or not. 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. Your if condition will never be true and never reach the DownloadFile statement.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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