Re: How to use get -delete ... to delete files only and not directories
Thank you,
I found a script which gets files and deletes them after succesfull download, it keeps the local directories hierarchy by default :)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
FtpSecure = [WinSCP.FtpSecure]::Implicit
HostName = "xxxx"
UserName = "xxxx"
Password = "xxxxx"
PortNumber = "990"
FtpMode = "Passive"
TlsHostCertificateFingerprint = "xxxxxx"
}
$session = New-Object WinSCP.Session
$session.SessionLogPath = "x:\xxxx.log"
try
{
# Connect
$session.Open($sessionOptions)
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.FileMask = "*"
$transferOptions.PreserveTimestamp = $true
$transferResult = $session.GetFiles("/","x:\xxxx", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host ("Download of {0} succeeded" -f $transfer.FileName)
#$removalResult = $session.RemoveFiles($session.EscapeFileMask($transfer.FileName))
#if ($removalResult.IsSuccess)
#{
#Write-Host ("Removing of file {0} succeeded" -f
#$transfer.FileName)
#}
#else
#{
#Write-Host ("Removing of file {0} failed" -f
#$transfer.FileName)
#}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}