James Studer
James Studer

Reputation: 37

WinSCP Move file after upload with C#

I am creating an SFTP upload program. It is working great, it connects to the remote SFTP server and uploads the files as intended. The issue I am having it I want the files once uploaded moved to a new directory on the local server. I have searched WinSCP site and did google searches, but the code I am up with it not working. Here is what I have:

foreach (TransferEventArgs transfer in transferResult.Transfers)
{
    Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
    session.MoveFile(transfer.FileName, Local_Processed);
}

In the log it states that it is moving the files but the files remain in the original folder and nothing appears in the processed folder.

Upvotes: 0

Views: 4462

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202138

The Session.MoveFile is for moving a remote file to another remote directory or for renaming a remote file. It's not for moving a remote file to a local directory.

To move a remote file to a local directory, use the remove parameter of the Session.GetFiles.


Though for me it looks like you actually want to move an original local file (that was uploaded) to another local directory. So it has actually nothing to do with WinSCP.

To move a local file, use the File.Move:

File.Move(transfer.FileName, destinationPath);

Upvotes: 1

James Studer
James Studer

Reputation: 37

Here is what ended up with after Martin Prikryl posted. I ended up having to add a second foreach after my first one used to just move the files. I also found that the *.* in my original original directory call had to be left out as this was also causing issues.

I ended up creating a second variable in my app.config file. It had the exact same path as the original directory variable except it didn't have the *.* for file name.

foreach (var file in Directory.GetFiles(OrgPath))
{
    File.Move(file, Path.Combine(Processed, Path.GetFileName(file)));
}

Upvotes: 0

Related Questions