• Loading...
ComponentPro UltimateSftp

Move files and directories

Language Filter: AllSend comments on this topic to ComponentPro

Move files and directories to another location on the same server

To move remote files and directories from one remote directory to another remote directory, use the Move methods. These methods require the path to the remote directory containing files and directories to move and the path of the destination remote directory. There are many advanced options such as moving files and directories recursively, custom search conditions, and custom file comparison.

Use the Move method to move remote files and directories to another location on the same server only.
using System;
using ComponentPro;
using ComponentPro.Net;
using ComponentPro.IO;

...

static void Main()
{
    // Create a new class instance. 
    Sftp client = new Sftp();

    // Connect to the SSFTP server. 
    client.Connect("myserver");

    // Authenticate. 
    client.Authenticate("userName", "password");

    // ... 
 
    // Move an entire directory with the standard rename command. 
    client.Rename("/my old folder", "/my new folder/new one");
    
    TransferOptions opt = new TransferOptions(
        true, // Build directory structure. 
        true, // Recursive. 
        OptionValue.Yes, // Create empty directories.   
        (SearchCondition)null, //  
        FileOverwriteMode.Overwrite,
        SymlinksResolveAction.Skip);
    opt.DeleteEmptyDirectories = true; // Remove empty directories.             
    // Move all .cs and .vb files to a new folder. 
    client.Move("/myfolder2/*.cs,*.vb", "/my new folder", opt);

    // Move the selected files in '/my folder' to 'my new folder'. 
    string[] files = { "my file1.dat", "file2.txt", "my dir" };
    client.Move("/my folder", files, "my new folder", opt);

    // Handle file existence event 
    client.TransferConfirm += client_TransferConfirm;
    opt.FileOverwriteMode = FileOverwriteMode.Confirm;

    // Move all *.dat and *.txt files to 'my dest' folder. 
    // If a .txt file already exists, skip it; otherwise overwrite. See the event handler client_TransferConfirm. 
    client.Move("my source/*.dat,*.txt", "my dest", opt);

    // With advanced search conditions. 
    opt.SearchCondition =
        SearchCondition.SizeInRange(10, 10240) +        // Search for files with size from 10 -> 10240 bytes 
        SearchCondition.ModifiedAfter(new DateTime(2010, 05, 01)); // and modifed after 2010/05/01. 
    // Move XLS files. 
    client.Move("my data/*.xls", "my dest", opt);

    // Move files simultaneously using 3 threads. 
    client.Move("/my root/*.txt", "/new folder", 3, true);

    // ... 
 
    // Disconnect. 
    client.Disconnect();
}

static void client_TransferConfirm(object sender, TransferConfirmEventArgs e)
{
    if (e.ConfirmReason == TransferConfirmReason.FileAlreadyExists)
    {
        if (e.SourceFileInfo.Name.EndsWith(".txt"))
            e.NextAction = TransferConfirmNextActions.Skip; // Skip 'txt' file if it already exists. 
        else 
            e.NextAction = TransferConfirmNextActions.Overwrite; // otherwise overwrite it. 
    }
}
To understand more about wildcard masks and search criterion, see this topic.

Move files and directories to another file system or vice versa

If you want to move files from local disk or another file system to the server or vice versa, use the Upload or Download method with the MoveFiles property of a TransferOptions object set to true. We take advantage of those methods with the following examples for the corresponding cases.

Move files and directories from a local disk to the remote server

using (Sftp client = new Sftp())
{
    client.Connect("myserver");
    client.Authenticate("userName", "password");
    
    TransferOptions opt = new TransferOptions();
    opt.MoveFiles = true;
    // Move files from "c:\data" to "/data" 
    client.Upload(@"c:\data", "/data", opt);

    // Move .dat files from "c:\data2" to "/data" 
    client.Upload(@"c:\data2\*.dat", "/data", opt);
}

Move remote files and directories to a local disk

using (Sftp client = new Sftp())
{
    client.Connect("myserver");
    client.Authenticate("userName", "password");

    TransferOptions opt = new TransferOptions();
    opt.MoveFiles = true;
    // Move files from "/data" to "c:\data" 
    client.Download("/data", @"c:\data", opt);

    // Move .dat files from "/data2" to "c:\data" 
    client.Upload(@"/data2/*.dat", @"c:\data", opt);
}

Move files and directories from another file system

using (Sftp client = new Sftp())
{
    client.Connect("myserver");
    client.Authenticate("userName", "password");

    TransferOptions opt = new TransferOptions();
    opt.MoveFiles = true;

    // Open a ZIP archive 
    Zip zip = new Zip();
    zip.Open("myZip.zip");

    // Move files from "/data" in the ZIP archive to "/data" 
    client.Upload(zip, "/data", false, null, "/data", opt);

    // Move .dat files from "/data2"  in the ZIP archive to "/data" 
    client.Upload(zip, "/data2/*.dat", false, null, "/data", opt);

    zip.Close();
}

Move remote files and directories to another file system

using (Sftp client = new Sftp())
{
    client.Connect("myserver");
    client.Authenticate("userName", "password");

    TransferOptions opt = new TransferOptions();
    opt.MoveFiles = true;

    // Open a ZIP archive 
    Zip zip = new Zip();
    zip.Open("myZip.zip");

    // Move files from "/data" in the ZIP archive to "/data" 
    client.Upload(zip, "/data", false, null, "/data", opt);

    // Move .dat files from "/data2"  in the ZIP archive to "/data" 
    client.Upload(zip, "/data2/*.dat", false, null, "/data", opt);

    zip.Close();
}