This is an old revision of the document!

Recursively moves files in directory tree to/from SFTP/FTP server while preserving source directory structure

When moving files to/from the server, WinSCP by defaults moves the subfolders too (removes them from the source directory).

If you want to preserve the source directory structure, you have to implement walking the source explicitly, moving file one by one, and thus preserving the directory structure.

Advertisement

Upload

The upload examples rely on Session.TranslateLocalPathToRemote which will be available in the upcoming WinSCP 5.8.3. You can implement your alternative instead meanwhile.

C#

Use the DirectoryInfo.EnumerateFileSystemInfos method to walk the source local tree.

using System;
using System.Collections.Generic;
using System.IO;
using WinSCP;
 
class Example
{
    public static int Main()
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "example.com",
                UserName = "user",
                Password = "mypassword",
                SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            };
 
            string localPath = @"C:\data";
            string remotePath = "/home/user/backup";
 
            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);
 
                // Enumerate files and directories to upload
                IEnumerable<FileSystemInfo> fileInfos = new DirectoryInfo(localPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories);
 
                foreach (FileSystemInfo fileInfo in fileInfos)
                {
                    string remoteFilePath = session.TranslateLocalPathToRemote(fileInfo.FullName, localPath, remotePath);
                    if (fileInfo.Attributes.HasFlag(FileAttributes.Directory))
                    {
                        // Create remote subdirectory, if it does not exist yet
                        if (!session.FileExists(remoteFilePath))
                        {
                            session.CreateDirectory(remoteFilePath);
                        }
                    }
                    else
                    {
                        Console.WriteLine(string.Format("Moving file {0}...", fileInfo.FullName));
                        // Upload file and remove original
                        session.PutFiles(fileInfo.FullName, remoteFilePath, true).Check();
                    }
                }
            }
 
            return 0;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e);
            return 1;
        }
    }
}

Advertisement

Last modified: by martin