0 votes
by (190 points)
edited

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
by (73.5k points)
edited by

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)
by (240 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
by (148k 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
by (140 points)
edited

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.

by (73.5k points)
edited

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.

...