FTP - how to overwrite older files OR files with different size

0 votes
asked Sep 3, 2013 by Rebex KB (8,190 points)
edited Sep 3, 2013

I'd like to use the FTP component to download different files only. I can see there is an option to download (and overwrite) EITHER files with different size OR older files:

client.Download(remote, local, traversal, transfer, ActionOnExistingFiles.OverwriteDifferentSize);

or

client.Download(remote, local, traversal, transfer, ActionOnExistingFiles.OverwriteOlder);

How can I combine these two conditions together?

Applies to: Rebex FTP/SSL, Rebex SFTP

1 Answer

0 votes
answered Sep 3, 2013 by Jan Sotola (16,920 points)
edited Sep 3, 2013
 
Best answer

To accomplish your need, you have to handle the overwriting manually in the ProblemDetected event handler.

Create a handler method like this:

static void client_ProblemDetected(object sender, FtpProblemDetectedEventArgs e)  
{ 
   // if the file does not exist, we cannot resolve what to do, so do nothing
   if (e.ProblemType != TransferProblemType.FileExists) 
         return;

   // first handle the file size
   if (e.RemoteItem.Length != e.LocalItem.Length)  
   { 
         e.Overwrite();             // overwrite unconditionally
         return; 
   }

   // then let the Overwrite method decide the file date and time
   // (the same as using ActionOnExistingFiles.OverwriteOlder in Download method)
   e.Overwrite(OverwriteCondition.Older); // overwrite conditionally, otherwise skip the file
}

Then assign this method to the ProblemDetected event of your FTP object before calling the Download method:

// connect and login
var client = new Ftp();
client.Connect("server");
client.Login("username", "password");

// assign the event handler
ftp.ProblemDetected += client_ProblemDetected;

// perform the file transfers
// you can (or rather should) omit the ActionOnExistingFiles argument now
ftp.Download(remotePath, localPath);

Note: This article can be used for the Rebex SFTP component as well - just replace the first line of the code with:

var client = new Sftp();
...