This is an old revision of the document!

Recursively move 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

PowerShell

param (
    # Use Generate URL function to obtain a value for -sessionUrl parameter.
    [Parameter(Mandatory)]
    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/",
    [Parameter(Mandatory)]
    $remotePath,
    [Parameter(Mandatory)]
    $localPath
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options from URL
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.ParseUrl($sessionUrl)
 
    $session = New-Object WinSCP.Session
    $session.SessionLogPath = "session.log"
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Enumerate files and directories to upload
        $files = Get-ChildItem $localPath -Recurse | Select-Object -ExpandProperty FullName
 
        foreach ($localFilePath in $files)
        {
            $remoteFilePath = $session.TranslateLocalPathToRemote($localFilePath, $localPath, $remotePath)
 
            if (Test-Path $localFilePath -PathType container)
            {
                # Create remote subdirectory, if it does not exist yet
                if (!($session.FileExists($remoteFilePath)))
                {
                    $session.CreateDirectory($remoteFilePath)
                }
            }
            else
            {
                Write-Host ("Moving file {0} to {1}..." -f $localFilePath, $remoteFilePath)
                # Upload file and remove original
                $session.PutFiles($localFilePath, $remoteFilePath, $True).Check()
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}

Advertisement

Download

For a download, you can use the code from the Recursively download directory tree with custom error handling example.

Just pass a true to the optional remove parameter of the Session.GetFiles.

C#

session.GetFiles(session.EscapeFileMask(fileInfo.FullName), localFilePath, true);

PowerShell

$session.GetFiles($session.EscapeFileMask($fileInfo.FullName), $localFilePath, $True)

Last modified: by martin