Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
3.3k views
in .NET FTP by (160 points)
Hello,
I am learning how to use this software with VB .net and I have a question that I do not seem to be able to find the answer to.
I'm trying to find out how exactly to download an entire folder with files in it.
I saw the Getfiles() and GetFileInfos() but I have absolutely no idea how to use them.
Can someone give me an easy to understand example of downloading a folder using edtftpnet, the free version?
Thanks in advance.

3 Answers

0 votes
by (162k points)
The simplest approach is to use recursion. List a directory via GetFileInfos(), get all the files, and for each folder recurse and do the same thing.
0 votes
by (160 points)
The simplest approach is to use recursion. List a directory via GetFileInfos(), get all the files, and for each folder recurse and do the same thing.

Thanks for the reply,
However, I am new to VB .net and I would really appreciate it if you (or anyone else who might be viewing this topic) can give me an example of using that method to download a folder from an ftp server, I mean an example code, not just telling me how to do it. :)
0 votes
by (1.5k points)
This might help you.
Its part of a recursive file copy routine from one of my apps but it should give you ideas so you can modify to suit.

You'll see that there is a switch 'fRecursive' which enables or disables the recursive ability. If enabled, it re-enters at the line -
If Not RecursiveDirectoryCopy(sDirInfo.FullName, dDirInfo.FullName, fRecursive, overWrite) Then

I've trimmed out a fair bit of code thats irrelevant to you but I think this should still function ok.


Public Function RecursiveDirectoryCopy(ByVal sourceDir As String, ByVal destDir As String, ByVal fRecursive As Boolean, ByVal overWrite As Boolean) As Boolean
Dim str As String
Dim sDir As String
Dim sDirInfo As IO.DirectoryInfo
Dim dDirInfo As IO.DirectoryInfo
Dim sFile As String
Dim sFileInfo As IO.FileInfo
Dim dFileInfo As IO.FileInfo

' Add trailing separators to the supplied paths if they don't exist.
If Not sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
sourceDir &= System.IO.Path.DirectorySeparatorChar
End If
If Not destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
destDir &= System.IO.Path.DirectorySeparatorChar
End If

'If destination directory does not exist, create it.
dDirInfo = New System.IO.DirectoryInfo(destDir)
If dDirInfo.Exists = False Then dDirInfo.Create()
dDirInfo = Nothing

' Recursive switch to enable drilling down into directory structure.
If fRecursive Then
' Get a list of directories from the current parent.
For Each sDir In System.IO.Directory.GetDirectories(sourceDir)
sDirInfo = New System.IO.DirectoryInfo(sDir)
dDirInfo = New System.IO.DirectoryInfo(destDir & sDirInfo.Name)
' Create the directory if it does not exist.
If dDirInfo.Exists = False Then dDirInfo.Create()
' Since we are in recursive mode, copy the children also
If Not RecursiveDirectoryCopy(sDirInfo.FullName, dDirInfo.FullName, fRecursive, overWrite) Then
Return False
End If
sDirInfo = Nothing
dDirInfo = Nothing
Next
End If

' Get the files from the current parent.
Try
For Each sFile In System.IO.Directory.GetFiles(sourceDir)
If Not bHalted Then 'user cancelled?
sFileInfo = New System.IO.FileInfo(sFile)
dFileInfo = New System.IO.FileInfo(Replace(sFile, sourceDir, destDir))
progbar_2.Value = CInt(IIf((iFiles >= Me.progbar_2.Maximum), Me.progbar_2.Maximum, iFiles))
iFiles += 1
str = "Copying " & sFileInfo.Name & " to " & dFileInfo.FullName
AddToRtfLog(str & " - " & iFiles & " of " & iFileCount, Color.Black)
AddTolblStatus("Copying files to the local buffer..." & vbNewLine & " " & iFiles & " of " & iFileCount, Color.Aqua)
Me.Refresh()

'If File does not exist. Copy it to buffer.
Dim iAttempts As Integer = 0
If dFileInfo.Exists = False Then
Try
If CopyProgress(sFileInfo.FullName, dFileInfo.FullName) Then
'increment 'copied' counter as real filecount flag - no copies= no zip
iCopyCount += 1
str = "Copied " & sFileInfo.Name & " to " & dFileInfo.FullName
AddToRtfLog(str, Color.Green)
Else
GoTo cleanup
End If
Catch ex As Exception
AddToRtfLog("RecursiveDirectoryCopy - CopyProgress error" & vbNewLine & _
" Error is - " & ex.Message.ToString, Color.Red)
End Try

End If
sFileInfo = Nothing
dFileInfo = Nothing
Else
GoTo cleanup
End If
Next

Catch ex As Exception
rejectRootFolder = sourceFileFolder
AddToRtfLog("Error in drop folder " & sourceDir & vbNewLine & _
"This folder will be deleted. Error is - " & ex.Message.ToString, Color.Red)
Return False
End Try

Return True

cleanup:
str = "Halting cloning process..."
AddToRtfLog(str, Color.Black)
Try
sFileInfo = Nothing
dFileInfo = Nothing
Catch ex As Exception
'
End Try
Return False

End Function 'RecursiveDirectoryCopy

Categories

...