Local files not being deleted during synchronization (PowerShell)

FF-SFTPHelp
Guest

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:
$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,

foreach ($download in$synchronizationResult.Downloads)
{
# Success or error?
if ($download.Error -eq$Null)
{
Write-Host "Download of $($download.FileName) succeeded, removing 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)")
}
}
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!

FF-SFTPHelp
Guest

[Resolved]

After much head banging I figured out what was going on.

It was two things really, but here's what I ended up with:
# Iterate over every download
foreach ($upload in$synchronizationResult.Uploads)
{
# Success or error?
if ($upload.Error -eq$Null)
{
Write-Host "Upload of $($upload.FileName) succeeded, removing from source"
# Upload succeeded, remove file from source
$filename = ($upload.FileName)
rm $filename if ($?)
{
Write-Host "Removing of file $($upload.FileName) succeeded"
}
else
{
Write-Host "Removing of file $($upload.FileName) failed"
}
}
else
{
Write-Host (
"Upload of $($upload.FileName) failed: $($upload.Error.Message)")
}
Firstly, I was referencing $synchronizationResult.Donwloads rather than Uploads, so I adjusted all of my references for that (and updated my comments). Secondly, I was using $session.RemoveFiles($filename) to remove the local file, which would not work because it was prepending my file name with the root directory of the remote FTP server. Instead I used a straight up rm command to remove the local file, and the $? variable to determine success or failure of that operation.

Anyway, I didn't find anything to point this out to me, so I thought I'd share in case others run across this.

You can post new topics in this forum