SFTP download list

0 votes
asked Jan 26, 2013 by gwolf2u (480 points)
edited Jan 28, 2013

So I want to download two or more files from a list asynchronously I have to code below

Public Sub Download()
Dim manifests = "1.manifest,2.manifest,3.manifest"
Dim remote_folder = "root\manifests"
Dim manifestlist As New List(Of String)
Dim splitit As String() = Split(manifests, ",")
For Each Split As String In splitit
   manifestlist.Add(Split)
Next

For Each Data As String In manifestlist
   Dim result As IAsyncResult = FTP.BeginDownload(remote_folder & "\" & Data, Application.StartupPath & "\dl", TraversalMode.Recursive, TransferMethod.Copy, ActionOnExistingFiles.OverwriteDifferentSize, New AsyncCallback(AddressOf Finish), Nothing)
Next
End Sub

Private Sub Finish(ByVal result As IAsyncResult)
 Msgbox("Done adding manifests")
End Sub

So the method should download files one by one, but this way it will download all files at same time. I want it to download first file, wait for finish, then second file and again wait for finish and as soon as last file is done, it should get the Finish sub.

Any idea on this one?

Applies to: Rebex SFTP

1 Answer

0 votes
answered Jan 28, 2013 by Lukas Matyska (55,430 points)
edited Jan 28, 2013
 
Best answer

The easiest way is to download all files in one method call using the Rebex.IO.FileSet class like this:

Private Sub Download()
    ' define file set to be downloaded
    Dim fileSet = New FileSet("root\manifests")

    Dim manifests = "1.manifest,2.manifest,3.manifest"
    Dim files As String() = Split(manifests, ",")
    For Each file As String In files
        fileSet.Include(file)
    Next

    ' download defined file set
    FTP.BeginDownload(fileSet, Path.Combine(Application.StartupPath, "dl"),
        TransferMethod.Copy, ActionOnExistingFiles.OverwriteDifferentSize,
        New AsyncCallback(AddressOf Finish), Nothing)
End Sub

Private Sub Finish(ByVal result As IAsyncResult)
    Try
        ' end the operation
        FTP.EndDownload(result)

        MsgBox("Done adding manifests")
    Catch ex As Exception
        MsgBox("Exception occured during adding manifests: " & ex.ToString())
    End Try
End Sub

If you want to do more subsequent operations asynchronously, I suggest you to do it in the way I described in my article about asynchronous programming alternative.

commented Jan 28, 2013 by gwolf2u (480 points)
edited Jan 28, 2013

thank you for your help easy to implement and understand

...