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.

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)


Topic review


Re: Deleting files off remote ftp folder after download

First, if you want to download and delete files, just use simple:
$session.GetFilesToDirectory($remotePath, $localPath, $Null, $True).Check()

That's all it takes. No need for that complicate code (which is about synchronization, not plain download).
If this does not work, add $session.ListDirectory($remotePath) after the $session.GetFilesToDirectory and post a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Deleting files off remote ftp folder after download

I am trying to delete files off a remote ftp server after successful download using PowerShell. I found this example and tried it:
In the console it shows that the file was deleted, but after refreshing the folder the files are still there.

Any help would be appreciated.
param (
    $localPath = "C:\backup\",
    $remotePath = "/home/user/data/"
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = ""
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
    $session = New-Object WinSCP.Session
        # Connect
        # Synchronize files to local directory, collect results
        $synchronizationResult = $session.SynchronizeDirectories(
            [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $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"
                    Write-Host "Removing of file $($download.FileName) failed"
                Write-Host (
                    "Download of $($download.FileName) failed: $($download.Error.Message)")
        # Disconnect, clean up
    exit 0
    Write-Host "Error: $($_.Exception.Message)"
    exit 1