Differences
This shows you the differences between the selected revisions of the page.
2020-12-18 | 2020-12-21 | ||
created (martin) | Simplified for 5.18.1 with use of .Reverse() and (implied) .ToString() (martin) | ||
Line 19: | Line 19: | ||
# @description Synchronizes files on local and remote directories including file deletions ^ | # @description Synchronizes files on local and remote directories including file deletions ^ | ||
# by remembering a list of previous local files | # by remembering a list of previous local files | ||
- | # @version 1 | + | # @version 2 |
# @homepage ~~SELF~~ | # @homepage ~~SELF~~ | ||
- | # @require WinSCP 5.17 | + | # @require WinSCP 5.18.1 |
# @option - -run group "Directories" | # @option - -run group "Directories" | ||
# @option LocalPath -run textbox "&Local directory:" "!\" | # @option LocalPath -run textbox "&Local directory:" "!\" | ||
Line 61: | Line 61: | ||
$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 | ||
Line 88: | Line 88: | ||
$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 104: | Line 104: | ||
if ($previousFiles -contains $difference.Local.FileName) | if ($previousFiles -contains $difference.Local.FileName) | ||
{ | { | ||
- | $path = $difference.Local.FileName | + | $difference.Reverse() |
- | Write-Host "Removing local file $path..." | + | |
- | Remove-Item -Recurse $path | + | |
} | } | ||
else | else | ||
{ | { | ||
- | Write-Host "Uploading new $($difference.Local.FileName)..." | ||
- | $difference.Resolve($session) | Out-Null | ||
$needRefresh = $True | $needRefresh = $True | ||
} | } | ||
Line 122: | Line 118: | ||
if ($previousFiles -contains $localFilePath) | if ($previousFiles -contains $localFilePath) | ||
{ | { | ||
- | $path = $difference.Remote.FileName | + | $difference.Reverse() |
- | Write-Host "Removing remote file $path..." | + | |
- | $path = [WinSCP.RemotePath]::EscapeFileMask($path) | + | |
- | $session.RemoveFiles($path).Check() | + | |
$needRefresh = $True | $needRefresh = $True | ||
} | } | ||
else | else | ||
{ | { | ||
- | Write-Host "Downloading new $($difference.Remote.FileName)..." | + | # noop |
- | $difference.Resolve($session) | Out-Null | + | |
} | } | ||
} | } | ||
elseif ($action -eq [WinSCP.SynchronizationAction]::DownloadUpdate) | elseif ($action -eq [WinSCP.SynchronizationAction]::DownloadUpdate) | ||
{ | { | ||
- | Write-Host "Downloading updated $($difference.Remote.FileName)..." | + | # noop |
- | $difference.Resolve($session) | Out-Null | + | |
} | } | ||
elseif ($action -eq [WinSCP.SynchronizationAction]::UploadUpdate) | elseif ($action -eq [WinSCP.SynchronizationAction]::UploadUpdate) | ||
{ | { | ||
- | Write-Host "Uploading updated $($difference.Local.FileName)..." | ||
- | $difference.Resolve($session) | Out-Null | ||
$needRefresh = $True | $needRefresh = $True | ||
} | } | ||
Line 148: | Line 137: | ||
{ | { | ||
throw "Unexpected difference $action" | throw "Unexpected difference $action" | ||
+ | } | ||
+ | |||
+ | Write-Host -NoNewline "$difference ..." | ||
+ | try | ||
+ | { | ||
+ | $difference.Resolve($session) | Out-Null | ||
+ | Write-Host -NoNewline " Done." | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | Write-Host | ||
} | } | ||
} | } | ||
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." | ||