I searched all over the internet about my question but I found answer for C# only, but none for VB.NET.
I want to download all the files and sub-directories of a directory on my FTP server.
I am currently doing it by downloading a ZIP file from my FTP server and extracting it but this is not a good method.
Thanks!
Translating my answer to C# Download all files and subdirectories through FTP to VB.NET:
The
FtpWebRequest
does not have any explicit support for recursive file operations (including downloads). You have to implement the recursion yourself:Tricky part is to identify files from subdirectories. There's no way to do that in a portable way with the
FtpWebRequest
. TheFtpWebRequest
unfortunately does not support theMLSD
command, which is the only portable way to retrieve directory listing with file attributes in FTP protocol. See also Checking if object on FTP server is file or directory.Your options are:
LIST
command =ListDirectoryDetails
method) and try to parse a server-specific listing. Many FTP servers use *nix-style listing, where you identify a directory by thed
at the very beginning of the entry. But many servers use a different format. The following example uses this approach (assuming the *nix format)Use the function like:
If you want to avoid troubles with parsing the server-specific directory listing formats, use a 3rd party library that supports the
MLSD
command and/or parsing variousLIST
listing formats; and recursive downloads.For example with WinSCP .NET assembly you can download whole directory with a single call to the
Session.GetFiles
:Internally, WinSCP uses the
MLSD
command, if supported by the server. If not, it uses theLIST
command and supports dozens of different listing formats.The
Session.GetFiles
method is recursive by default.(I'm the author of WinSCP)
I KNOW YOU MIGHT BE LAZY TO READ ALL THAT, BUT THIS IS THE ANSWER!!! THE CODE IS AT THE END :D
Hello,
It is very simple to do using the WinSCP library which is available for C# and VB.NET - here is the full answer:
Firsly, install the WinSCP library to your project, see here: https://winscp.net/eng/docs/library#downloading_and_installing_the_assembly
Or if you are lazy to do it, then just download the library and add the DLL file as a reference in your project.
And now let's download the whole directory including its files and its sub-directories to the local storage.
You have 2 options:
Session.GetFiles to download even if the files & directories are already downloaded.
Session.SynchronizeDirectories to download the files & directories if they does not exists, and also will download the the modified files which already exists. (BEST and better than the first option).
I used the second option of course because it is the best one ever. However, if you want to use the Session.GetFiles method... you can see the VB.NET example here: https://winscp.net/eng/docs/library_session_getfiles#vbnet
And here is how I used the second option: https://winscp.net/eng/docs/faq_script_modified_files
As you can see, everything is explained great!
And as I wanted to download the files from the FTP Server to my local PC, then here is what I used (SynchronizeDirectories):
Imports WinSCP
first :-)Use this code, because the code on https://winscp.net/eng/docs/library_session_synchronizedirectories#vbnet is for SFTP (port 22) and not FTP (port 21) ... and also it uses
SynchronizeMode.Remote
which uploads from your PC to the FTP server , so in the code below I replaced.Remote
with.Local
Don't forget to replace the credentials and the paths..
One more thing? Good luck on your project! :-)