How to move between folders on SFTP server?

0 votes
asked Nov 15, 2012 by Rebex KB (8,210 points)
edited Nov 15, 2012

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

Applies to: Rebex SFTP

1 Answer

0 votes
answered Nov 15, 2012 by Jan Sotola (16,930 points)
edited Nov 15, 2012
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.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.