Ignore locked files on download

Advertisement

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);
}

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum