0 votes
by (8.6k points)
edited

How can I move files or folders to another folder on the same SFTP server?

Applies to: Rebex SFTP

1 Answer

0 votes
by (18.1k points)
edited
 
Best answer

To move an individual file or a whole directory, you can simply use the Sftp.Rename method.

Here are several samples:
(similar methods can be used also for the Rebex FTP/SSL component)

// connect an log in the SFTP server
var client = new Sftp();
client.Connect("hostname");
client.Login("username", "password");

Renaming and moving files

Rename a file:

client.Rename("file.txt", "file2.txt");

Move a file to another directory:

client.Rename("file.txt", "subdir/file.txt");

Rename a file and move it to another directory:

client.Rename("file.txt", "subdir/file2.txt");

Renaming and moving directories

Rename a directory:

client.Rename("mydir", "mydir2");

Move a directory to another directory:

client.Rename("mydir", "subdir/mydir");

Rename a directory and move it to another directory:

client.Rename("mydir", "subdir/mydir2");

Mass moving set of files

Unfortunately you cannot use rename to mass move, but it can be easily done as follows:

// move all .txt files from 'one' directory to 'two' directory                   
SftpItemCollection items = client.GetItems("one/*.txt");
foreach (var item in items)
{
    client.Rename("one/" + item.Name, "two/" + item.Name);
}

NOTE: The above article describes moving files or directories within one SFTP server only.
Moving of files from local computer to an SFTP server (or backwards) is described in another article.

...