Differences
This shows you the differences between the selected revisions of the page.
library_session_synchronizedirectories 2015-12-14 | library_session_synchronizedirectories 2024-02-15 (current) | ||
Line 1: | Line 1: | ||
====== Session.SynchronizeDirectories Method ====== | ====== Session.SynchronizeDirectories Method ====== | ||
- | [[task_synchronize_full|Synchronizes directories]]. | + | [[task_synchronize_full|Synchronizes]] content of a local directory with a remote one or vice versa or mutually. |
===== Syntax ===== | ===== Syntax ===== | ||
Line 16: | Line 16: | ||
<code vbnet *> | <code vbnet *> | ||
- | Public Function SynchronizeDirectories( _ | + | Public Function SynchronizeDirectories( |
- | ByVal mode As SynchronizationMode, _ | + | mode As SynchronizationMode, |
- | ByVal localPath As String, _ | + | localPath As String, |
- | ByVal remotePath As String, _ | + | remotePath As String, |
- | ByVal removeFiles As Boolean, _ | + | removeFiles As Boolean, |
- | ByVal Optional mirror As Boolean = False, _ | + | Optional mirror As Boolean = False, |
- | ByVal Optional criteria As SynchronizationCriteria = 1, _ | + | Optional criteria As SynchronizationCriteria = SynchronizationCriteria.Time, |
- | ByVal Optional options As TransferOptions = Null _ | + | Optional options As TransferOptions = Nothing |
) As SynchronizationResult | ) As SynchronizationResult | ||
</code> | </code> | ||
- | ==== Parameters ==== | + | ==== [[parameters]] Parameters ==== |
^ Name ^ Description ^ | ^ Name ^ Description ^ | ||
- | | SynchronizationMode mode | [[task_synchronize_full#synchronization_mode|Synchronization mode]]. Possible values are ''SynchronizationMode.Local'', ''SynchronizationMode.Remote'' and ''SynchronizationMode.Both''. | | + | | SynchronizationMode ==mode== | [[task_synchronize_full##direction|Synchronization direction]]. Possible values are ''SynchronizationMode.Local'', ''SynchronizationMode.Remote'' and ''SynchronizationMode.Both''. | |
- | | string localPath | Full path to local directory. | | + | | string ==localPath== | Full path to local directory. | |
- | | string remotePath | Full path to remote directory. | | + | | string ==remotePath== | Full path to remote directory. | |
- | | bool removeFiles | When set to ''true'', deletes obsolete files. Cannot be used for ''SynchronizationMode.Both''. | | + | | bool ==removeFiles== | When set to ''true'', deletes obsolete files. Cannot be used for ''SynchronizationMode.Both''. | |
- | | bool mirror | When set to ''true'', synchronizes in [[task_synchronize_full#synchronization_mode|mirror mode]] (synchronizes also older files). Cannot be used for ''SynchronizationMode.Both''. Defaults to ''false''. | | + | | bool ==mirror== | When set to ''true'', synchronizes in [[task_synchronize_full#mode|mirror mode]] (synchronizes also older files). Cannot be used for ''SynchronizationMode.Both''. Defaults to ''false''. | |
- | | SynchronizationCriteria criteria | [[ui_synchronize#comparison_criteria|Comparison criteria]]. Possible values are ''SynchronizationCriteria.None'', ''SynchronizationCriteria.Time'' (default), ''SynchronizationCriteria.Size'' and ''SynchronizationCriteria.Either''. For ''SynchronizationMode.Both'' ''SynchronizationCriteria.Time'' can be used only. | | + | | SynchronizationCriteria ==criteria== | [[ui_synchronize#criteria|Comparison criteria]]. The parameter is a bit field of any of ''SynchronizationCriteria.Time'' (default), ''SynchronizationCriteria.Size'' and ''SynchronizationCriteria.Checksum''. ''SynchronizationCriteria.None'' can be used as an alias to empty bit field. For backward compatibility, ''SynchronizationCriteria.Either'' can be used as an alias to ''Time %%|%% Size''. For ''SynchronizationMode.Both'', ''SynchronizationCriteria.Time'' can be used only. | |
- | | [[library_transferoptions|TransferOptions]] options | Transfer options. Defaults to ''null'', what is equivalent to ''new TransferOptions()''. | | + | | [[library_transferoptions|TransferOptions]] ==options== | Transfer options. Defaults to ''null'', what is equivalent to ''new TransferOptions()''. | |
==== Return Value ==== | ==== Return Value ==== | ||
Line 47: | Line 47: | ||
| TimeoutException | Timeout waiting for ''winscp.com'' to respond. | | | TimeoutException | Timeout waiting for ''winscp.com'' to respond. | | ||
- | ===== Remarks ===== | + | ===== [[remarks]] Remarks ===== |
Event ''[[library_session_filetransferred|Session.FileTransferred]]'' is raised for every uploaded or downloaded file. | Event ''[[library_session_filetransferred|Session.FileTransferred]]'' is raised for every uploaded or downloaded file. | ||
+ | |||
+ | To further customize the synchronization, use instead a combination of [[library_session_comparedirectories|''Session.CompareDirectories'']] and [[library_comparisondifference_resolve|''ComparisonDifference.Resolve'']]. This allows synchronizing only selected differences, changing synchronization order or customizing error handling. | ||
===== [[example]] Examples ===== | ===== [[example]] Examples ===== | ||
+ | |||
==== [[csharp]] C# Example ==== | ==== [[csharp]] C# Example ==== | ||
<code csharp> | <code csharp> | ||
Line 69: | Line 72: | ||
UserName = "user", | UserName = "user", | ||
Password = "mypassword", | Password = "mypassword", | ||
- | SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
}; | }; | ||
Line 84: | Line 87: | ||
synchronizationResult = | synchronizationResult = | ||
session.SynchronizeDirectories( | session.SynchronizeDirectories( | ||
- | SynchronizationMode.Remote, @"d:\www", "/home/martin/public_html", false); | + | SynchronizationMode.Remote, @"d:\www", |
+ | ·······················"/home/martin/public_html", false); | ||
// Throw on any error | // Throw on any error | ||
Line 114: | Line 118: | ||
if (e.Chmod.Error == null) | if (e.Chmod.Error == null) | ||
{ | { | ||
- | Console.WriteLine("Permisions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions); | + | Console.WriteLine( |
+ | ····················"Permissions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions); | ||
} | } | ||
else | else | ||
{ | { | ||
- | Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error); | + | Console.WriteLine( |
+ | ····················"Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error); | ||
} | } | ||
} | } | ||
Line 130: | Line 136: | ||
if (e.Touch.Error == null) | if (e.Touch.Error == null) | ||
{ | { | ||
- | Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime); | + | Console.WriteLine( |
+ | ····················"Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime); | ||
} | } | ||
else | else | ||
{ | { | ||
- | Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error); | + | Console.WriteLine( |
+ | ····················"Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error); | ||
} | } | ||
} | } | ||
Line 140: | Line 148: | ||
{ | { | ||
// This should never happen during "local to remote" synchronization | // This should never happen during "local to remote" synchronization | ||
- | Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination); | + | Console.WriteLine( |
+ | ················"Timestamp of {0} kept with its default (current time)", e.Destination); | ||
} | } | ||
} | } | ||
Line 148: | Line 157: | ||
==== [[vbnet]] VB.NET Example ==== | ==== [[vbnet]] VB.NET Example ==== | ||
<code vbnet> | <code vbnet> | ||
- | Imports System | ||
Imports WinSCP | Imports WinSCP | ||
Line 157: | Line 165: | ||
Try | Try | ||
' Setup session options | ' Setup session options | ||
- | Dim mySessionOptions As New SessionOptions | + | Dim sessionOptions As New SessionOptions |
- | With mySessionOptions | + | With sessionOptions |
.Protocol = Protocol.Sftp | .Protocol = Protocol.Sftp | ||
.HostName = "example.com" | .HostName = "example.com" | ||
.UserName = "user" | .UserName = "user" | ||
.Password = "mypassword" | .Password = "mypassword" | ||
- | .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
End With | End With | ||
- | Using mySession As Session = New Session | + | Using session As New Session |
' Will continuously report progress of synchronization | ' Will continuously report progress of synchronization | ||
- | AddHandler mySession.FileTransferred, AddressOf FileTransferred | + | AddHandler session.FileTransferred, AddressOf FileTransferred |
' Connect | ' Connect | ||
- | mySession.Open(mySessionOptions) | + | session.Open(sessionOptions) |
' Synchronize files | ' Synchronize files | ||
- | Dim mySynchronizationResult As SynchronizationResult | + | Dim synchronizationResult As SynchronizationResult |
- | mySynchronizationResult = _ | + | synchronizationResult = |
- | mySession.SynchronizeDirectories( _ | + | session.SynchronizeDirectories( |
SynchronizationMode.Remote, "d:\www", "/home/martin/public_html", False) | SynchronizationMode.Remote, "d:\www", "/home/martin/public_html", False) | ||
' Throw on any error | ' Throw on any error | ||
- | mySynchronizationResult.Check() | + | synchronizationResult.Check() |
End Using | End Using | ||
Line 191: | Line 199: | ||
End Function | End Function | ||
- | Private Shared Sub FileTransferred(ByVal sender As Object, ByVal e As TransferEventArgs) | + | Private Shared Sub FileTransferred(sender As Object, e As TransferEventArgs) |
If e.Error Is Nothing Then | If e.Error Is Nothing Then | ||
Line 201: | Line 209: | ||
If e.Chmod IsNot Nothing Then | If e.Chmod IsNot Nothing Then | ||
If e.Chmod.Error Is Nothing Then | If e.Chmod.Error Is Nothing Then | ||
- | Console.WriteLine("Permisions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions) | + | Console.WriteLine( |
+ | ····················"Permissions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions) | ||
Else | Else | ||
- | Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error) | + | Console.WriteLine( |
+ | ····················"Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error) | ||
End If | End If | ||
Else | Else | ||
Line 211: | Line 221: | ||
If e.Touch IsNot Nothing Then | If e.Touch IsNot Nothing Then | ||
If e.Touch.Error Is Nothing Then | If e.Touch.Error Is Nothing Then | ||
- | Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime) | + | Console.WriteLine( |
+ | ····················"Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime) | ||
Else | Else | ||
- | Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error) | + | Console.WriteLine( |
+ | ····················"Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error) | ||
End If | End If | ||
Else | Else | ||
' This should never happen during "local to remote" synchronization | ' This should never happen during "local to remote" synchronization | ||
- | Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination) | + | Console.WriteLine( |
+ | ················"Timestamp of {0} kept with its default (current time)", e.Destination) | ||
End If | End If | ||
Line 240: | Line 253: | ||
if ($e.Error -eq $Null) | if ($e.Error -eq $Null) | ||
{ | { | ||
- | Write-Host ("Upload of {0} succeeded" -f $e.FileName) | + | Write-Host "Upload of $($e.FileName) succeeded" |
} | } | ||
else | else | ||
{ | { | ||
- | Write-Host ("Upload of {0} failed: {1}" -f $e.FileName, $e.Error) | + | Write-Host "Upload of $($e.FileName) failed: $($e.Error)" |
} | } | ||
Line 251: | Line 264: | ||
if ($e.Chmod.Error -eq $Null) | if ($e.Chmod.Error -eq $Null) | ||
{ | { | ||
- | Write-Host ("Permisions of {0} set to {1}" -f $e.Chmod.FileName, $e.Chmod.FilePermissions) | + | Write-Host "Permissions of $($e.Chmod.FileName) set to $($e.Chmod.FilePermissions)" |
} | } | ||
else | else | ||
{ | { | ||
- | Write-Host ("Setting permissions of {0} failed: {1}" -f $e.Chmod.FileName, $e.Chmod.Error) | + | Write-Host "Setting permissions of $($e.Chmod.FileName) failed: $($e.Chmod.Error)" |
} | } | ||
Line 261: | Line 274: | ||
else | else | ||
{ | { | ||
- | Write-Host ("Permissions of {0} kept with their defaults" -f $e.Destination) | + | Write-Host "Permissions of $($e.Destination) kept with their defaults" |
} | } | ||
Line 268: | Line 281: | ||
if ($e.Touch.Error -eq $Null) | if ($e.Touch.Error -eq $Null) | ||
{ | { | ||
- | Write-Host ("Timestamp of {0} set to {1}" -f $e.Touch.FileName, $e.Touch.LastWriteTime) | + | Write-Host "Timestamp of $($e.Touch.FileName) set to $($e.Touch.LastWriteTime)" |
} | } | ||
else | else | ||
{ | { | ||
- | Write-Host ("Setting timestamp of {0} failed: {1}" -f $e.Touch.FileName, $e.Touch.Error) | + | Write-Host "Setting timestamp of $($e.Touch.FileName) failed: $($e.Touch.Error)" |
} | } | ||
Line 279: | Line 292: | ||
{ | { | ||
# This should never happen during "local to remote" synchronization | # This should never happen during "local to remote" synchronization | ||
- | Write-Host ("Timestamp of {0} kept with its default (current time)" -f $e.Destination) | + | Write-Host "Timestamp of $($e.Destination) kept with its default (current time)" |
} | } | ||
} | } | ||
Line 287: | Line 300: | ||
try | try | ||
{ | { | ||
- | $sessionOptions = New-Object WinSCP.SessionOptions | + | $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ |
- | ···$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | + | ········Protocol = [WinSCP.Protocol]::Sftp |
- | $sessionOptions.HostName = "example.com" | + | ·······HostName = "example.com" |
- | $sessionOptions.UserName = "user" | + | ·······UserName = "user" |
- | $sessionOptions.Password = "mypassword" | + | ·······Password = "mypassword" |
- | $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | ·······SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
+ | } | ||
$session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
Line 318: | Line 332: | ||
exit 0 | exit 0 | ||
} | } | ||
- | catch [Exception] | + | catch |
{ | { | ||
- | Write-Host $_.Exception.Message | + | Write-Host "Error: $($_.Exception.Message)" |
exit 1 | exit 1 | ||
} | } | ||
Line 350: | Line 364: | ||
if (e.Chmod.Error == null) | if (e.Chmod.Error == null) | ||
{ | { | ||
- | WScript.Echo("Permisions of " + e.Chmod.FileName + " set to " + e.Chmod.FilePermissions); | + | WScript.Echo( |
+ | ················"Permissions of " + e.Chmod.FileName + " set to " + e.Chmod.FilePermissions); | ||
} | } | ||
else | else | ||
{ | { | ||
- | WScript.Echo("Setting permissions of " + e.Chmod.FileName + " failed: " + e.Chmod.Error); | + | WScript.Echo( |
+ | ················"Setting permissions of " + e.Chmod.FileName + " failed: " + e.Chmod.Error); | ||
} | } | ||
} | } | ||
Line 366: | Line 382: | ||
if (e.Touch.Error == null) | if (e.Touch.Error == null) | ||
{ | { | ||
- | WScript.Echo("Timestamp of " + e.Touch.FileName + " set to " + e.Touch.LastWriteTime); | + | WScript.Echo( |
+ | ················"Timestamp of " + e.Touch.FileName + " set to " + e.Touch.LastWriteTime); | ||
} | } | ||
else | else | ||
{ | { | ||
- | WScript.Echo("Setting timestamp of " + e.Touch.FileName + " failed: " + e.Touch.Error); | + | WScript.Echo( |
+ | ················"Setting timestamp of " + e.Touch.FileName + " failed: " + e.Touch.Error); | ||
} | } | ||
} | } | ||
Line 376: | Line 394: | ||
{ | { | ||
// This should never happen during "local to remote" synchronization | // This should never happen during "local to remote" synchronization | ||
- | WScript.Echo("Timestamp of " + e.Destination + " kept with its default (current time)"); | + | WScript.Echo( |
+ | ············"Timestamp of " + e.Destination + " kept with its default (current time)"); | ||
} | } | ||
} | } | ||
Line 390: | Line 409: | ||
sessionOptions.UserName = "user"; | sessionOptions.UserName = "user"; | ||
sessionOptions.Password = "mypassword"; | sessionOptions.Password = "mypassword"; | ||
- | sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; | + | sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."; |
var session = WScript.CreateObject("WinSCP.Session", "session_"); | var session = WScript.CreateObject("WinSCP.Session", "session_"); | ||
Line 417: | Line 436: | ||
WScript.Quit(1); | WScript.Quit(1); | ||
} | } | ||
+ | |||
</script> | </script> | ||
</job> | </job> | ||
</code> | </code> | ||
- | ==== Real-Life Example ==== | + | ==== [[reallife]] Real-Life Example ==== |
- | + | ||
- | * [[library_example_delete_after_successful_download|Deleting remote files after successful remote to local synchronization]]. | + | |
+ | * [[library_example_delete_after_successful_download|*]]. | ||