Differences
This shows you the differences between the selected revisions of the page.
| library_example_delete_after_successful_download 2014-08-04 | library_example_delete_after_successful_download 2022-06-16 (current) | ||
| Line 1: | Line 1: | ||
| ====== Deleting remote files after successful remote to local synchronization ====== | ====== Deleting remote files after successful remote to local synchronization ====== | ||
| - | //For background, see article [[faq_delete_synchronized_files|How do I create script that synchronizes files and deletes synchronized files from source afterward?]]// | + | //For background, see article [[faq_delete_synchronized_files|*]]// |
| - | If you do not have your favorite language, use [[library_powershell|PowerShell]]: | + | The following example uses [[library|WinSCP .NET assembly]] from a [[library_powershell|PowerShell]] script. If you have another preferred language, you can easily translate it. |
| <code powershell> | <code powershell> | ||
| + | param ( | ||
| + | $localPath = "C:\backup\", | ||
| + | $remotePath = "/home/user/data/" | ||
| + | ) | ||
| + | |||
| try | try | ||
| { | { | ||
| Line 12: | Line 17: | ||
| # Setup session options | # Setup session options | ||
| - | $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 26: | Line 32: | ||
| $session.Open($sessionOptions) | $session.Open($sessionOptions) | ||
| - | $localPath = "C:\backup\" | ||
| - | $remotePath = "/home/user/data/" | ||
| - | |||
| # Synchronize files to local directory, collect results | # Synchronize files to local directory, collect results | ||
| $synchronizationResult = $session.SynchronizeDirectories( | $synchronizationResult = $session.SynchronizeDirectories( | ||
| Line 46: | Line 49: | ||
| if ($download.Error -eq $Null) | if ($download.Error -eq $Null) | ||
| { | { | ||
| - | Write-Host ("Download of {0} succeeded, removing from source" -f | + | Write-Host "Download of $($download.FileName) succeeded, removing from source" |
| - | $download.FileName) | + | |
| # Download succeeded, remove file from source | # Download succeeded, remove file from source | ||
| - | $removalResult = $session.RemoveFiles($session.EscapeFileMask($download.FileName)) | + | $filename = [WinSCP.RemotePath]::EscapeFileMask($download.FileName) |
| + | $removalResult = $session.RemoveFiles($filename) | ||
| if ($removalResult.IsSuccess) | if ($removalResult.IsSuccess) | ||
| { | { | ||
| - | Write-Host ("Removing of file {0} succeeded" -f | + | Write-Host "Removing of file $($download.FileName) succeeded" |
| - | ························$download.FileName) | + | |
| } | } | ||
| else | else | ||
| { | { | ||
| - | Write-Host ("Removing of file {0} failed" -f | + | Write-Host "Removing of file $($download.FileName) failed" |
| - | ························$download.FileName) | + | |
| } | } | ||
| } | } | ||
| else | else | ||
| { | { | ||
| - | Write-Host ("Download of {0} failed: {1}" -f | + | Write-Host ( |
| - | ···················$download.FileName, $download.Error.Message) | + | ····················"Download of $($download.FileName) failed: $($download.Error.Message)") |
| } | } | ||
| } | } | ||
| Line 77: | Line 78: | ||
| exit 0 | exit 0 | ||
| } | } | ||
| - | catch [Exception] | + | catch |
| { | { | ||
| - | Write-Host $_.Exception.Message | + | Write-Host "Error: $($_.Exception.Message)" |
| exit 1 | exit 1 | ||
| } | } | ||