Advanced Download and Upload with FTP and SFTP components

  |   Tomas Knopp

In the Basic Download and Upload with FTP and SFTP components blog post, we described how to download multiple files or directories with Rebex FTP and Rebex SFTP*. Today, we are going to explain TraversalMode as an additional option for Download and Upload methods.

TraversalMode specifies how to search for items (options are SHALLOW or DEEP) and how to transfer directories (options are RECURSIVE or NON-RECURSIVE). Available TraversalMode values are:

  • Recursive (default) - SHALLOW search; transfers files normally; directories are transferred recursively (with all contents)
  • NonRecursive - SHALLOW search; transfers files normally; directories are transferred non-recursively (creates empty directories)
  • MatchFilesShallow - SHALLOW search; transfers files only
  • MatchFilesDeep - DEEP search; transfers files only

Let's consider following remote structure:

/data/file1.txt
/data/file2.txt
/data/file1.pdf
/data/file2.pdf
/data/backup/
/data/backup/file3.txt
/data/backup/file4.txt
/data/backup/file3.pdf
/data/backup/file4.pdf

1) Downloading files from directory (subdirectories are not searched), i.e. MatchFilesShallow mode

If you want to download all files from the "/data" directory only (and not from subdirectories). This can be done using the TraversalMode.MatchFilesShallow:

client.Download("/data/*", @"C:\data", TraversalMode.MatchFilesShallow);

This will result in following local structure:

C:\data\file1.txt
C:\data\file2.txt
C:\data\file1.pdf
C:\data\file2.pdf

If you want to download only .txt files (" *.txt"), it can be done like this:

client.Download("/data/*.txt", @"C:\data", TraversalMode.MatchFilesShallow);

And the result will be:

C:\data\file1.txt
C:\data\file2.txt

2) Downloading files from directory and all its subdirectories, i.e. MatchFilesDeep mode

If you want to download all .txt files (" *.txt") from directory "/data" and also from all its subdirectories the TraversalMode.MatchFilesDeep can be used:

client.Download("/data/*.txt", @"C:\data", TraversalMode.MatchFilesDeep);

The local structure will be:

C:\data\file1.txt
C:\data\file2.txt
C:\data\backup\
C:\data\backup\file3.txt
C:\data\backup\file4.txt

3) Using Recursive and NonRecursive mode

To understand how the recursive and non-recursive mode works, we decided to show you results of all possible combinations:

a) Recursive and " *" mask:

client.Download("/data/*", @"C:\data", TraversalMode.Recursive);

C:\data\file1.txt
C:\data\file2.txt
C:\data\file1.pdf
C:\data\file2.pdf
C:\data\backup\ 
C:\data\backup\file3.txt
C:\data\backup\file4.txt
C:\data\backup\file3.pdf
C:\data\backup\file4.pdf

b) Recursive and " *.txt" mask:

client.Download("/data/*.txt", @"C:\data", TraversalMode.Recursive);

C:\data\file1.txt
C:\data\file2.txt

If you want to download also files from "backup" directory, use MatchFilesDeep instead.

c) Recursive and a directory:

client.Download("/data", @"C:\data", TraversalMode.Recursive);

C:\data\data\file1.txt
C:\data\data\file2.txt
C:\data\data\file1.pdf
C:\data\data\file2.pdf
C:\data\data\backup\ 
C:\data\data\backup\file3.txt
C:\data\data\backup\file4.txt
C:\data\data\backup\file3.pdf
C:\data\data\backup\file4.pdf

If you don't want to create another "data" directory locally, use "/data/ *" instead.

d) NonRecursive and " *" mask:

client.Download("/data/*", @"C:\data", TraversalMode.NonRecursive);

C:\data\file1.txt
C:\data\file2.txt
C:\data\file1.pdf
C:\data\file2.pdf
C:\data\backup\

If you don't want to create empty "backup" directory locally, use MatchFilesShallow instead.

e) NonRecursive and " *.txt" mask:

client.Download("/data/*.txt", @"C:\data", TraversalMode.NonRecursive);

C:\data\file1.txt
C:\data\file2.txt

f) NonRecursive and a directory:

client.Download("/data", @"C:\data", TraversalMode.NonRecursive);

C:\data\data 

If you want to download "/data" directory content non-recursively, use "/data/*" instead.

*Our Rebex ZIP component also offers the new multi-file API. The ZipArchive.Add and ZipArchive.Extract methods also support wildcards, FileSet and all the Add/Extract overloads can be used in the same way as Ftp or Sftp client's Upload/Download multi-file methods described in this blogpost.