Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

Guest

Ignore locked files on download

Hi there!

I made a service, which checks every 5 minutes, if there are new files on an FTP server and if yes, it downloads it. However, if another user is currently uploading a file, the download fails, even if there are other files on the server. Is it possible to have a download for all these files which aren't locked and to just ignore the locked files, so they can be downloaded at the next time, instead of waiting that all files are being unlocked and downloading all of them by then?

Here's my code:
try

{
   SessionOptions sessionOptions = new SessionOptions();
   if (isSFTP)
   {
      sessionOptions.Protocol = Protocol.Sftp;
   }
   else
   {
      sessionOptions.Protocol = Protocol.Ftp;
   }
   sessionOptions.HostName = ftpUrl;
   sessionOptions.UserName = ftpUserName;
   sessionOptions.Password = ftpPassword;
   //options.SshHostKeyFingerprint =
   sessionOptions.PortNumber = ftpPort;
            
   TransferOperationResult transferOperationResult;

   using (Session session = new Session())
   {
      session.Open(sessionOptions);

      TransferOptions transferOptions = new TransferOptions();
      transferOptions.TransferMode = TransferMode.Binary;
      transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;

      // Download the files
      // This fails, if any file is locked, rather than just downloading the unlocked files
      transferOperationResult = session.GetFiles(ftpDirectory, targetPath, false, transferOptions);

      // Validate if the result is ok or throw an exception, if there were any errors during the transfer operation.
      transferOperationResult.Check();

      if (transferOperationResult.IsSuccess)
      {
         if(transferOperationResult.Transfers.Count > 0)
         {
            _log.Info("{0} were downloaded!", transferOperationResult.Transfers);
            foreach (TransferEventArgs transfer in transferOperationResult.Transfers)
            {
               RemovalOperationResult removalResult = session.RemoveFiles(RemotePath.EscapeFileMask(transfer.FileName));

               if (!removalResult.IsSuccess)
               {
                  _log.Error("Couldn't delete the file from the server: {0}", transfer.FileName);
               }
            }
         }
         else
         {
            _log.Trace("No files found.");
         }
      }
      else
      {
         _log.Error("Download incomplete!");
      }
   }
}
catch(SessionLocalException ex)
{
   _log.Error("WinSCP: There was an error communicating with winscp process.winscp cannot be found or executed.");
   _log.Error(ex);
}
catch(SessionRemoteException ex)
{
   _log.Error(ex);
}
catch (Exception ex)
{
   _log.Error(ex);
}