Differences
This shows you the differences between the selected revisions of the page.
library_example_two_way_synchronize_delete 2020-12-21 | library_example_two_way_synchronize_delete 2022-06-16 (current) | ||
Line 1: | Line 1: | ||
- | ====== Two-Way Synchronization with Delete with SFTP/FTP server====== | + | ====== Two-way synchronization with delete with SFTP/FTP server·====== |
WinSCP [[task_synchronize_full|synchronization functionality]] is state-less. So when there is a file on one side, which is absent on the opposite side, WinSCP is not able to determine, if the file was added on the first side, or removed on the latter. For this reason in the [[task_synchronize_full#direction|//Both// direction]], WinSCP does not support the //Delete files// option. | WinSCP [[task_synchronize_full|synchronization functionality]] is state-less. So when there is a file on one side, which is absent on the opposite side, WinSCP is not able to determine, if the file was added on the first side, or removed on the latter. For this reason in the [[task_synchronize_full#direction|//Both// direction]], WinSCP does not support the //Delete files// option. | ||
Line 9: | Line 9: | ||
<code batch> | <code batch> | ||
- | powershell.exe -File C:\path\SynchronizeTwoWayDelete.ps1 -sessionUrl "sftp://user:password@example.com/" -localPath "C:\local" -remotePath "/remote" -listPath "C:\cache\example.txt" | + | powershell.exe -File C:\path\SynchronizeTwoWayDelete.ps1 -sessionUrl "sftp://user:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/" -localPath "C:\local" -remotePath "/remote" -listPath "C:\cache\example.txt" |
</code> | </code> | ||
Line 31: | Line 31: | ||
# @option SessionLogPath -config sessionlogfile | # @option SessionLogPath -config sessionlogfile | ||
# @optionspage ~~SELF~~#options | # @optionspage ~~SELF~~#options | ||
- | · | + | |
param ( | param ( | ||
# Use Generate Session URL function to obtain a value for -sessionUrl parameter. | # Use Generate Session URL function to obtain a value for -sessionUrl parameter. | ||
- | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/", | + | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/", |
[Parameter(Mandatory = $True)] | [Parameter(Mandatory = $True)] | ||
$localPath, | $localPath, | ||
Line 47: | Line 47: | ||
$refresh | $refresh | ||
) | ) | ||
- | · | + | |
try | try | ||
{ | { | ||
Line 57: | Line 57: | ||
$sessionOptions = New-Object WinSCP.SessionOptions | $sessionOptions = New-Object WinSCP.SessionOptions | ||
$sessionOptions.ParseUrl($sessionUrl) | $sessionOptions.ParseUrl($sessionUrl) | ||
- | · | + | |
$listPath = [Environment]::ExpandEnvironmentVariables($listPath) | $listPath = [Environment]::ExpandEnvironmentVariables($listPath) | ||
$listDir = (Split-Path -Parent $listPath) | $listDir = (Split-Path -Parent $listPath) | ||
New-Item -ItemType directory -Path $listDir -Force | Out-Null | New-Item -ItemType directory -Path $listDir -Force | Out-Null | ||
- | · | + | |
if (Test-Path $listPath) | if (Test-Path $listPath) | ||
{ | { | ||
Line 72: | Line 72: | ||
$previousFiles = @() | $previousFiles = @() | ||
} | } | ||
- | · | + | |
$needRefresh = $False | $needRefresh = $False | ||
$session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
- | ···· | + | |
try | try | ||
{ | { | ||
$session.SessionLogPath = $sessionLogPath | $session.SessionLogPath = $sessionLogPath | ||
- | · | + | |
Write-Host "Connecting..." | Write-Host "Connecting..." | ||
$session.Open($sessionOptions) | $session.Open($sessionOptions) | ||
- | · | + | |
Write-Host "Comparing files..." | Write-Host "Comparing files..." | ||
$differences = | $differences = | ||
$session.CompareDirectories( | $session.CompareDirectories( | ||
[WinSCP.SynchronizationMode]::Both, $localPath, $remotePath, $False) | [WinSCP.SynchronizationMode]::Both, $localPath, $remotePath, $False) | ||
- | · | + | |
if ($differences.Count -eq 0) | if ($differences.Count -eq 0) | ||
{ | { | ||
Line 96: | Line 96: | ||
{ | { | ||
Write-Host "Synchronizing $($differences.Count) change(s)..." | Write-Host "Synchronizing $($differences.Count) change(s)..." | ||
- | · | + | |
foreach ($difference in $differences) | foreach ($difference in $differences) | ||
{ | { | ||
Line 157: | Line 157: | ||
$session.Dispose() | $session.Dispose() | ||
} | } | ||
- | · | + | |
# Refresh the remote directory in WinSCP GUI, if it was changed and -refresh switch was used | # Refresh the remote directory in WinSCP GUI, if it was changed and -refresh switch was used | ||
if ($refresh -and $needRefresh) | if ($refresh -and $needRefresh) | ||
Line 164: | Line 164: | ||
& "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath" | & "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath" | ||
} | } | ||
- | · | + | |
Write-Host "Saving current local file list..." | Write-Host "Saving current local file list..." | ||
$localFiles = | $localFiles = | ||
Line 170: | Line 170: | ||
Select-Object -ExpandProperty FullName | Select-Object -ExpandProperty FullName | ||
Set-Content $listPath $localFiles | Set-Content $listPath $localFiles | ||
- | · | + | |
Write-Host "Done." | Write-Host "Done." | ||
- | ···· | + | |
$result = 0 | $result = 0 | ||
} | } | ||
Line 180: | Line 180: | ||
$result = 1 | $result = 1 | ||
} | } | ||
- | · | + | |
# Pause if -pause switch was used | # Pause if -pause switch was used | ||
if ($pause) | if ($pause) | ||
Line 187: | Line 187: | ||
[System.Console]::ReadKey() | Out-Null | [System.Console]::ReadKey() | Out-Null | ||
} | } | ||
- | · | + | |
exit $result | exit $result | ||
</code> | </code> |