GetFiles Method Has Started Attempting to Delete Remote Directory

Advertisement

tmiddleton
Joined:
Posts:
3
Location:
Tulsa

GetFiles Method Has Started Attempting to Delete Remote Directory

We started getting permission denied exceptions out of nowhere the other night. It's coming from the remote server when we try to remove the remote files after downloading. We are using GetFiles method with the Remove flag set to true. There were no code changes on our end that I know of and at the time of first occurance we were running 5.7.1. I found a similar bug report and upgraded to 5.7.5 but the behavior has not changed. Is there a way to disable this behavior? I only want the files removed not the folder.

I apologize in advance if this this is already covered somewhere but I could find no other reference to this behavior nor any property I could set to turn it off.

I've attached the relevant portion of the log.

Reply with quote E-mail

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,454
Location:
Prague, Czechia

Re: GetFiles Method Has Started Attempting to Delete Remote Directory

What "this behaviour"?

Show us your code and the session log file (Session.SessionLogPath).

Reply with quote

tmiddleton
Joined:
Posts:
3
Location:
Tulsa

Hello, thank you for responding so quickly!

The behavior or action I want it to take is to delete the remote files that were successfully downloaded but not the remote directory. The login being used doesn't have sufficient privileges to remove a directory so the permission denied exception is being returned and causing the script to terminate with a failure code.

I've included the script below. The log I attached in the fist post is from the session log file. I'll get you a more complete version if needed asap.


$CurrentDir = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
$ProcessingFolder = "\\local\path\"

try
{
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
   
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = ""
    $sessionOptions.UserName = ""
    $sessionOptions.Password = 
    $sessionOptions.SshHostKeyFingerprint = ""
    $sessionOptions.Timeout = New-TimeSpan -Seconds 30

    $session = New-Object WinSCP.Session
    $session.SessionLogPath = "$CurrentDir\WinSCP.log"

    $session.Open($sessionOptions)
 
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Automatic
    $transferOptions.FilePermissions = $null
    $transferOptions.PreserveTimestamp = $False

    $transferResult = $session.GetFiles("/remote/path/", $ProcessingFolder, $True, $transferOptions)
    
    $transferResult.Check()
}
catch [Exception]
{
    ...Send Email...
    exit 1
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}

Reply with quote E-mail

martin
Site Admin
martin avatar
Joined:
Posts:
41,454
Location:
Prague, Czechia

tmiddleton wrote:

The behavior or action I want it to take is to delete the remote files that were successfully downloaded but not the remote directory. The login being used doesn't have sufficient privileges to remove a directory so the permission denied exception is being returned and causing the script to terminate with a failure code.
So download (and delete) the files only, not the parent directory:

$transferResult = $session.GetFiles("/remote/path/*", $ProcessingFolder, $True, $transferOptions)

Reply with quote

Advertisement

MarkovdWeide
Guest

Re: Exception with GetFiles() with Remove on Directory

martin wrote:


So download (and delete) the files only, not the parent directory:
$transferResult = $session.GetFiles("/remote/path/*", $ProcessingFolder, $True, $transferOptions)
I'd like to add something I encountered using this 'trick' real quick. Using the asterisk (*) does indeed preserve the directory on the remote server. However, I needed to use the asterisk in both the remotePath and localPath! Without an asterisk in the localPath the transfer failed stating that the localPath was "not a file!"

$transferResult = $session.GetFiles("/remote/path/*", "local/path/*", $True, $transferOptions)

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,454
Location:
Prague, Czechia

Re: Exception with GetFiles() with Remove on Directory

MarkovdWeide wrote:

I'd like to add something I encountered using this 'trick' real quick. Using the asterisk (*) does indeed preserve the directory on the remote server. However, I needed to use the asterisk in both the remotePath and localPath! Without an asterisk in the localPath the transfer failed stating that the localPath was "not a file!"
You do not need the asterisk, but you do need the trailing backslash: "local/path/".

Reply with quote

Advertisement

You can post new topics in this forum