Unfortunately, this kind of filtering in single Download()
call is not possible.
However, there are two ways, how to do this:
The most effective solution is to call _client.GetItems(FileSet)
and then call in loop _client.GetFile()
for each file.
Please note that calling _client.Download()
for single file has additional overhead.
Unfortunately, the FileSet
class can filter files based on relative paths only (not dates). However, this logic can be achieved, if you execute one additional GetItems()
to create "path-->item cache". Once the FileSet
has associated "path-->item cache", it can filter by any criteria.
The file set for second solution can look like this:
public class MyFileSet : Rebex.IO.FileSet
{
public Dictionary<string, FileSystemItem> ItemCache { get; private set; }
public bool IsFiltering { get; set; }
private bool _failOnNewlyDiscoveredFiles;
public MyFileSet(string basePath) : base(basePath.TrimEnd('/'))
{
ItemCache = new Dictionary<string, FileSystemItem>();
}
public void Reset()
{
ItemCache.Clear();
IsFiltering = false;
}
public override bool IsMatch(string relativePath, FileSetMatchMode mode)
{
if (IsFiltering && mode == FileSetMatchMode.MatchFile)
{
// prepare absolute path - used in cache
string absolutePath = string.Format("{0}/{1}", BasePath, relativePath);
FileSystemItem item;
if (ItemCache.TryGetValue(absolutePath, out item))
{
return Filter(item);
}
// the file was not found in cache - this is probably newly added file
if (_failOnNewlyDiscoveredFiles)
throw new InvalidOperationException(string.Format("Newly discovered file encountered '{0}'.", absolutePath));
else
return false; // skip it here, it can be transferred in future
}
return base.IsMatch(relativePath, mode);
}
private bool Filter(FileSystemItem item)
{
// implement filter logic here
return item.LastWriteTime >= new DateTime(2007, 03, 19, 20, 53, 00);
}
}
You can just rewrite the Filter(FileSystemItem)
method to include your filtering logic.
This file set can be used like this (working example, which connects to test.rebex.net site):
// connect to your server
var client = new FileTransferClient();
client.Connect("test.rebex.net", FileTransferMode.Sftp);
client.Login("demo", "password");
// prepare file set
var set = new MyFileSet("/pub/example"); // use absolute path here
set.Include("*.*", TraversalMode.MatchFilesDeep); // add some includes
// prepare item cache for later filtering
var items = client.GetItems(set);
foreach (var item in items.OrderBy(fsi => fsi.LastWriteTime))
{
// display info of all discovered items
Console.WriteLine("Item: {0} {1}", item.Path, item.LastWriteTime);
set.ItemCache.Add(item.Path, item);
}
// register event to display info of all transferred files
client.TransferProgressChanged += (s, e) =>
{
if (e.TransferState == TransferProgressState.FileTransferred)
Console.WriteLine("Trasnferred: {0} {1}", e.SourceItem.Path, e.SourceItem.LastWriteTime);
};
// download items with advanced filtering
set.IsFiltering = true;
client.Download(set, "c:/data");
The only overhead compared to client.Download(set)
call is the initial execution of client.GetItems(set)
.