Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Local files not being deleted during synchronization (PowerShell)

If your goal is to move all local files to a remote directory, you can do it with a single line of code:
$session.PutFilesToDirectory($localDir, $remoteDir, $Null, $True)

https://winscp.net/eng/docs/library_session_putfilestodirectory
FF-SFTPHelp

[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.
FF-SFTPHelp

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,
# 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)")
    }
}

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!