Local files not being deleted during synchronization (PowerShell)
Hey Folks,
I am in need of assistance in determining why local files aren't being deleted during a 'remote' transfer. The files present in the local directory are being transferred without issue, it's just the deletion that's being skipped.
I used a template as the basis for the PowerShell script, but here is the section in question:
I have a nearly duplicate section (different variables) that copies from the remote FTP server to a local directory and successfully removes the files from the remote FTP server.
This template was originally for copying from remote and deleting remote files afterward, so I'm thinking I'm missing something I should have changed for it to work in the opposite direction.
Any help is much appreciated!
I am in need of assistance in determining why local files aren't being deleted during a 'remote' transfer. The files present in the local directory are being transferred without issue, it's just the deletion that's being skipped.
I used a template as the basis for the PowerShell script, but here is the section in question:
$session.SessionLogPath = "C:\path\to\TransferLogPS.log" # Connect $session.Open($sessionOptions) # Synchronize files to Customer FTP server, collect results $synchronizationResult = $session.SynchronizeDirectories( [WinSCP.SynchronizationMode]::Remote, $localDir, $remoteDir, $False) # Deliberately not calling $synchronizationResult.Check # as that would abort our script on any error. # We will find any error in the loop below # (note that $synchronizationResult.Downloads is the only operation # collection of SynchronizationResult that can contain any items, # as we are not removing nor uploading anything) # Iterate over every download foreach ($download in $synchronizationResult.Downloads) { # Success or error? if ($download.Error -eq $Null) { Write-Host "Download of $($download.FileName) succeeded, removing from source" # Download succeeded, remove file from source $filename = [WinSCP.RemotePath]::EscapeFileMask($download.FileName) $removalResult = $session.RemoveFiles($filename) if ($removalResult.IsSuccess) { Write-Host "Removing of file $($download.FileName) succeeded" } else { Write-Host "Removing of file $($download.FileName) failed" } } else { Write-Host ( "Download of $($download.FileName) failed: $($download.Error.Message)") } }
This template was originally for copying from remote and deleting remote files afterward, so I'm thinking I'm missing something I should have changed for it to work in the opposite direction.
Any help is much appreciated!