0

I'm using the following code to download files from an FTP server. Before entering this code, I have created a list of filenames from files that are in the directory on the FTP server. There are over 2000 files in the list.

As I iterate through the list, the files download properly until I reach exactly 121 files downloaded. Then it starts giving me an error of "file not found, access denied." for every file after that. However the files are there. If I start the process over again it will pick up from where it left off and download another 121 files and continue until it errors again after the next 121 files.

Here is the code:

For Each file As String In dirlist
    DownloadFile(local_path + "\" + filename, new_path + "/" + Trim(filename), client)
Next


Private Sub DownloadFile(ByVal localpath As String, ByVal ftpuri As String, client As String)

    Dim request As New WebClient()
    request.Credentials = New NetworkCredential(user_name, password)
    Dim bytes() As Byte = request.DownloadData(ftpuri)

    Try
        Dim DownloadStream As FileStream = IO.File.Create(localpath)
        DownloadStream.Write(bytes, 0, bytes.Length)
        DownloadStream.Close()

    Catch ex As Exception
        add_to_log(log_window, ex.Message)
    End Try

End Sub

I do not understand why it is stopping before completing the list.

John
  • 1,310
  • 3
  • 32
  • 58
  • Wild guess: the FTP server has a limit of connections/downloads per session. Either that or you need to dispose your ´WebClient()´ object after each loop – Josh Part Sep 06 '17 at 20:50
  • Can you download more than 121 files at once using a standalone FTP client? Show us a log file of both the standalone client and your code (see https://stackoverflow.com/q/9664650/850848). – Martin Prikryl Sep 07 '17 at 06:00
  • @JoshPart, I assumed it was reaching some sort of limit (connections, buffer, whatever), I just couldn't figure out where the limit was being set. I added request.dispose() after each iteration and it is working now. Thanks. – John Sep 07 '17 at 21:08

0 Answers0