I have a PowerShell script that is working great to grab some files from a remote SFTP. But I can not get the code to delete the files after download. Here is what I have.
$session.Open($sessionOptions)
# Set up transfer options
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
ResumeSupport = New-Object WinSCP.TransferResumeSupport -Property @{ State = [WinSCP.TransferResumeSupportState]::Off }
}
# Transfer files
$session.GetFiles("/remote/*.txt", "\\Local\*", $False, $transferOptions).Check()
# Delete Transfers
$session.RemoveFiles($difference.Remote.FileName)
Instead I get an error that states:
Removals Failures
-------- -------- ---------
{} {WinSCP.SessionRemoteException: Error listing directory '"/Files'.... False
To delete the downloaded files from the server, just pass true
to the remove
(3rd) argument of Session.GetFiles
:
$session.GetFiles("/remote/*.txt", "C:\local\*", $True, $transferOptions).Check()