1

I'm using SSH.NET library for SFTP. I have a need to upload a file into two folders on same server, however the file will be large, so I'd rather not send it over the wire twice.

Is there a command to copy from one folder to the next once the data is on the server? Or perhaps to upload at the same time without having to send two copies of the same data over the wire?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Alex
  • 1,192
  • 14
  • 30

2 Answers2

1

You can use something like Powershell FileSystemWatcher to monitor the changes in one of your folders. Then once there is a new file added to that folder, you can trigger an action (e.g. Robocopy) and copy the file into the other folder.

Mahdi
  • 3,199
  • 2
  • 25
  • 35
1

You will probably not be able to copy the file directly. For details why, see:
In an SFTP session is it possible to copy one remote file to another location on same remote SFTP server?

If you have a shell access, you can of course execute cp command using a shell session.
See How to run commands on SSH server in C#?


The only reliably working way to duplicate remote file over SFTP is to download and re-upload the file.

The easiest way to do that (without creating a temporary local file) is:

SftpClient client = new SftpClient("example.com", "username", "password");
client.Connect();

using (Stream sourceStream = client.OpenRead("/source/path/file.dat"))
using (Stream destStream = client.Create("/dest/path/file.dat"))
{
    sourceStream.CopyTo(destStream);
}

But I'm aware that this is not what you are after.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992