Download all files from online FTP folder. I Would like to know the code how can i download all files from this online directory. Convert C# to VB.
UPDATE: This blogpost from year 2009 uses old API (GetFiles method), in the new versions of Rebex components there are the newer multiple file operations Upload and Download. See the multiple-file operations feature page for code samples in C# and VB.NET.
Quick & dirty (yet a bit naive) directory download code
To download all files in a specified remote folder seems to be a trivial task at first. Calling Ftp.GetList(), iterating through the returned collection and caling Ftp.GetFile() on each item looks like a no brainer. It would work in simple cases. However, there are some caveats in this approach. Consider the following situations:
- Remote directory has both files and folder with other files. Shouldwe download the content of subdirectories too?
- What if some of the remote files already exist on the localcomputer? Should they be overwritten? Skipped? Should we overwriteolder files only?
- What if the local file is not writable? Should the whole transferfail? Should we skip the file and continue to the next?
- How to handle files on a remote disk which are unreadable because wedon’t have sufficient access rights?
- How are the symlinks,hard links and junctionpoints handled?Links can easily be used to create an infinite recursive directorytreestructure. Consider folder A with subfolder B which in fact is notthe real folder but the *nix hard link pointing back to folder A.The naive approach will end in an application which never ends (atleast if nobody manage to pull the plug).
Download the directory – resolve the conflicts automatically
The above code solves all the issues mentioned above. The programmer decides how to resolve most conflicts. In this case existing files are overwritten, links are followed and downloaded correctly, infinite link loops are detected and handled by the component code by throwing an exception. Such code is best for unattended processing.
What if the user needs to choose which local files should be overwritten and which not? What if we want to skip files with certain type of problem? Let’s check another scenario and give this power to the hands of the user.
Download the directory – let the user resolve the conflicts
Each time the component reaches the point in which a decision has to be made the user is asked. A dialog pops up and the user is given an option to resolve the issue for the specific file or directory. He is also able to resolve all similar cases with one choice - e. g. he is given an option to either ‘Overwrite a specific file’ or ‘Overwrite all files if older’ as shown on the screeshot.
To see this approach implemented in a fully functional application download the component and check the FTP Batch Transfer Sample. Both C # and VB.NET code available.