Re: Compare - how to get the results?
Please try to implement it on your own and ask about any problems you encounter.
Couldn't we enumerate the differences and then let the necessary action (delete/upload) do afterwards?
GetFiles
/PutFiles
/RemoveFiles
as needed.
ComparisonDifference
that will do that automatically.
FileTransferred
is indeed called even during a synchronization. But it gives you a progress of individual file transfers.
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"
.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
End With
Using session As New Session
' Will continuously report progress of synchronization
AddHandler session.FileTransferred, AddressOf FileTransferred
' Connect
session.Open(sessionOptions)
' Synchronize files
Dim synchronizationResult As SynchronizationResult
synchronizationResult =
session.SynchronizeDirectories(
SynchronizationMode.Remote, "d:\www", "/home/martin/public_html", False)
' Throw on any error
synchronizationResult.Check()
End Using
Return 0
Catch e As Exception
Console.WriteLine("Error: {0}", e)
Return 1
End Try
End Function
Private Shared Sub FileTransferred(sender As Object, e As TransferEventArgs)
If e.Error Is Nothing Then
Console.WriteLine("Upload of {0} succeeded", e.FileName)
Else
Console.WriteLine("Upload of {0} failed: {1}", e.FileName, e.Error)
End If
If e.Chmod IsNot Nothing Then
If e.Chmod.Error Is Nothing Then
Console.WriteLine(
"Permissions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions)
Else
Console.WriteLine(
"Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error)
End If
Else
Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination)
End If
If e.Touch IsNot Nothing Then
If e.Touch.Error Is Nothing Then
Console.WriteLine(
"Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime)
Else
Console.WriteLine(
"Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error)
End If
Else
' This should never happen during "local to remote" synchronization
Console.WriteLine(
"Timestamp of {0} kept with its default (current time)", e.Destination)
End If
End Sub
End Class
Indeed, progress reporting for synchronization is not supported atm.
Session.FileTransferProgress
event is at its documentation page:
Session.GetFiles
. But there's no difference when using it with Session.PutFiles
.