How to check for successful transfer and delete remotely?

0 votes
asked Apr 4, 2011 by jenn2157 (190 points)
edited Mar 13, 2012

Hello, I am using the SFTP component and would like to know after I execute my client.getfiles() command to retrieve all PDF files, how do I check that they were transferred successfully and delete them off the remote server?

Thank you!

Applies to: Rebex SFTP

2 Answers

+2 votes
answered Apr 4, 2011 by Lukas Matyska (55,430 points)
edited Aug 16, 2018 by Lukas Pokorny

To detect whether the file was successfully transferred use the BatchTransferProgress event. In the callback method check the e.Operation property to SftpBatchTransferOperation.FileTransferred.

To delete remote files you can use this approach:

Sftp client = new Sftp();
client.Connect("server");
client.Login("user", "pwd");

client.BatchTransferProgress += new SftpBatchTransferProgressEventHandler(client_BatchTransferProgress);
client.GetFiles("data/*.pdf", @"c:\data", SftpBatchTransferOptions.Recursive);

void client_BatchTransferProgress(object sender, SftpBatchTransferProgressEventArgs e)
{
    if (e.Operation == SftpBatchTransferOperation.FileTransferred)
    {
        Sftp client = (Sftp)sender;
        client.DeleteFile(e.RemotePath);
    }
}

UPDATE: From Release 2012 R1 you can use new multi-file methods Download() and Upload() with the Rebex.IO.TransferMethod.Move argument to achieve exactly what you want. The file is deleted in source location after it was successfully transferred to the target destination.

client.Download("sourcemask", "targetpath", TraversalMode.Recursive, TransferMethod.Move, ActionOnExistingFiles.ThrowException)
commented Aug 16, 2018 by Vinay (220 points)
Hi,
 I am using 2018R2 trail version. I tried using below download method but it doesn't do anything and no error.
client.Download("source_mask", "target_path", TraversalMode.Recursive, TransferMethod.Move, 0)

And also I tried using 'BatchTransferProgress' example you shown above it downloaded all files but didn't delete all files from the remoter.

Please suggest.

Thanks ,
Vinay
commented Aug 16, 2018 by Lukas Pokorny (104,290 points)
Hi, please try running the following code (change targetPath first to an existing directory):

            string targetPath = @"c:\temp\downloads";

            using (var client = new Sftp())
            {
                client.LogWriter = new FileLogWriter("log.txt", LogLevel.Debug);
                client.Connect("test.rebex.net");
                client.Login("demo", "password");
                client.ChangeDirectory("/pub/example");
                client.GetRawList();
                client.Download("*.png", targetPath, TraversalMode.Recursive, TransferMethod.Copy, 0);
            }

If it works, try to modify it to download files from your server. If that still doesn't work, send us the communication log (log.txt) created by the application. If it does work, delete downloaded files, change TransferMethod.Copy to TransferMethod.Move and try again. If it doesn't delete the files this time, send us the communication log for analysis. Thanks!
0 votes
answered Jan 10, 2012 by jalmto (140 points)
edited Jan 10, 2012

Have you gotten this to work? I keep getting an error stating "Another operation is pending." I am in need of a similar solution and can figure out how to remove a file once it's been dowloaded in a batch.

commented Jan 10, 2012 by Lukas Matyska (55,430 points)
edited Jan 10, 2012

Please note this works for Sftp only. From the message "Another operation is pending." I guess you are using Ftp.

In Ftp you have to transfer each file one by one. Alternatively you can remember the e.RemotePath instead of deleting the file. When then whole transfer is finished, iterate through a collection of remembered paths and delete each file one by one.

Please note the Move functionality is already added into current build, but it is not released yet. If you would like to receive current beta version, please let me know.

...