This is an old revision of the document!
Session.GetFiles Method
Downloads one or more files from remote directory to local directory.
This feature will be available only with the next release.
Advertisement
Syntax
public TransferOperationResult GetFiles( string remotePath, string localPath, bool remove = false, TransferOptions options = null )
Parameters
Name | Description |
---|---|
string remotePath | Full path to remote file or directory to download. Filename in the path can be replaced with wildcard to select multiple files. When file name is omited (path ends with slash), all files and subdirectories in the remote directory are downloaded. |
string localPath | Full path to download the file to. When downloading multiple files, the filename in the path should be replaced with operation mask or omited (path ends with backslash). |
bool remove | When set to true , deletes source remote file(s) after transfer. Defaults to false . |
TransferOptions options | Transfer options. Defaults to null , what is equivalent to new TransferOptions() . |
Return Value
TransferOperationResult
. See also Capturing results of operations.
Advertisement
Exceptions
Exception | Condition |
---|---|
InvalidOperationException | Session is not opened. |
ArgumentException ArgumentOutOfRangeException |
Invalid combination of values of TransferOptions properties. |
SessionLocalException | Error communicating with winscp.com . See the exception documentation for details. |
TimeoutException | Timeout waiting for winscp.com to respond. |
Remarks
Event Session.FileTransferred
is raised for every downloaded file.
Example
C# Example
using System; using System.Globalization; using System.IO; using WinSCP; class Example { public static int Main() { try { // Setup session options SessionOptions sessionOptions = new SessionOptions(); sessionOptions.Protocol = Protocol.Sftp; sessionOptions.HostName = "example.com"; sessionOptions.UserName = "user"; sessionOptions.Password = "mypassword"; sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; using (Session session = new Session()) { // Connect session.Open(sessionOptions); string stamp = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture); string fileName = "export_" + stamp + ".txt"; string remotePath = "/home/user/sysbatch/" + fileName; string localPath = "d:\\backup\\" + fileName; // Manual "remote to local" synchronization. // You can achieve the same using: // session.SynchronizeDirectories( // SynchronizationMode.Local, localPath, remotePath, false, false, SynchronizationCriteria.Time, // new TransferOptions { IncludeMask = fileName }).Check(); if (session.FileExists(remotePath)) { bool download; if (!File.Exists(localPath)) { Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath); download = true; } else { DateTime remoteWriteTime = session.GetFileInfo(remotePath).LastWriteTime; DateTime localWriteTime = File.GetLastWriteTime(localPath); if (remoteWriteTime > localWriteTime) { Console.WriteLine( "File {0} as well as local backup {1} exist, " + "but remote file is newer ({2}) than local backup ({3})", remotePath, localPath, remoteWriteTime, localWriteTime); download = true; } else { Console.WriteLine( "File {0} as well as local backup {1} exist, " + "but remote file is not newer ({2}) than local backup local backup ({3})", remotePath, localPath, remoteWriteTime, localWriteTime); download = false; } } if (download) { // Download the file and throw on any error session.GetFiles(remotePath, localPath).Check(); Console.WriteLine("Download to backup done."); } } else { Console.WriteLine("File {0} does not exist yet", remotePath); } } return 0; } catch (Exception e) { Console.WriteLine("Error: {0}", e); return 1; } } }
Advertisement
VB.NET Example
Imports System Imports System.Globalization Imports System.IO Imports WinSCP Friend Class Example Public Shared Function Main() As Integer Try ' Setup session options Dim sessionOptions As New SessionOptions With sessionOptions .Protocol = Protocol.Sftp .HostName = "example.com" .UserName = "user" .Password = "mypassword" .SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" End With Using session As Session = New Session ' Connect session.Open(sessionOptions) Dim stamp As String = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture) Dim fileName As String = "export_" & stamp & ".txt" Dim remotePath As String = "/home/user/sysbatch/" & fileName Dim localPath As String = "d:\backup\" & fileName ' Manual "remote to local" synchronization. ' You can achieve the same using: ' session.SynchronizeDirectories( _ ' SynchronizationMode.Local, localPath, remotePath, False, False, SynchronizationCriteria.Time, _ ' New TransferOptions() { .IncludeMask = fileName }).Check If session.FileExists(remotePath) Then Dim download As Boolean If Not File.Exists(localPath) Then Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath) download = True Else Dim remoteWriteTime As DateTime = session.GetFileInfo(remotePath).LastWriteTime Dim localWriteTime As DateTime = File.GetLastWriteTime(localPath) If remoteWriteTime > localWriteTime Then Console.WriteLine( _ "File {0} as well as local backup {1} exist, " & _ "but remote file is newer ({2}) than local backup ({3})", _ remotePath, localPath, remoteWriteTime, localWriteTime) download = True Else Console.WriteLine( _ "File {0} as well as local backup {1} exist, " & _ "but remote file is not newer ({2}) than local backup local backup ({3})", _ remotePath, localPath, remoteWriteTime, localWriteTime) download = False End If End If If download Then ' Download the file and throw on any error session.GetFiles(remotePath, localPath).Check Console.WriteLine("Download to backup done.") End If Else Console.WriteLine("File {0} does not exist yet", remotePath) End If End Using Return 0 Catch e As Exception Console.WriteLine("Error: {0}", e) Return 1 End Try End Function End Class
Advertisement