Topic "Problem with NET Library 5.9 - Removal parameter $True"

Author Message
Alcasczar
[View user's profile]

Joined: 2016-07-30
Posts: 7
Location: NY
Winscp 5.9.0 .NET Library
Script running under Powershell 3.0
Using Sftp

When the below code runs an error appears that it cannot delete file, the file is not a file though. It's an Actual directory.


Code:


try

{

$logpath = 'C:\logs\Incoming'

if (!(test-path $logpath))

{

    new-item $logpath -ItemType Directory

}


# Load WinSCP .NET assembly

Add-Type -Path "C:\Winscp\WinSCPnet.dll"
$VerbosePreference = 'Continue'
 

$remotePath               = '/Test/Incoming/'
$localPath                = 'C:\Test\Incoming'

$HostKey = "ssh-rsa 2048 50:e8:d0:20:ea:45:98:28:7f:4w:59:89:b9:19:49:99"

 

[int]$port = 22

    # Setup session options

    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{

        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "myserver"
        PortNumber = $port
        UserName = "testuser"
        Password = "Secret"
        SshHostKeyFingerprint = $HostKey     

    }

    try

    {

    $session = New-Object WinSCP.Session

   
        # Connect

        $session.Open($sessionOptions)

        # Download files

        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
        $transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
        $transferOptions.FileMask = "*.*|.no_delete,*.tmp,*.part"

        $transferResult = $session.GetFiles($remotePath, $localPath, $True, $transferOptions)

        $transferResult.Check()

        foreach ($transfer in $transferResult.Transfers)
        {
             $Filename = $transfer.FileName
             Write-verbose ("Download of {0} succeeded" -f $transfer.FileName)
        }

        # Disconnect, clean up
        $session.Dispose()   
        Exit 0

}     
catch [exception]
{

        Write-Verbose ("Error: {0}" -f $_.Exception.Message)
        Exit 1
        }

}
catch {
   write-verbose " F A T A L  --  E R R O R"
   add-log $logpath -data $_

}

 


The remote side stores a hidden file on the server (Linux) called .no_delete (to protect directories from deletion) and as you can see we don't download that as it's in the exclusion FileMask option. The error that is displayed is


Code:
[8/3/2016 2:09 PM] An Error has occurred:  :  Error deleting file '/Test/Incoming/AccessPoint'.

General failure (server should provide error description).
Error code: 4
Error message from server (en-US): Directory is not empty

Common reasons for the Error code 4 are:

- Renaming a file to a name of already existing file.
- Creating a directory that already exists.
- Moving a remote file to a different filesystem (HDD).
- Uploading a file to a full filesystem (HDD).
- Exceeding a user disk quota.



The file that's mentioned in the error is not a file it's an actual Directory.

Any assistance is greatly appreciated.
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25015
Location: Prague, Czechia
Please attach 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.
Alcasczar
[View user's profile]

Joined: 2016-07-30
Posts: 7
Location: NY
martin wrote:
Please attach 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.


Attached is a session log.


Thanks!!
session-test-log.log (35.13 KB) Private file

Description: (none)

martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25015
Location: Prague, Czechia
Your SFTP server does not even return the .no_delete in the listing. So WinSCP is not aware of it. If the file is in the AccessPoint subfolder too, that explains why WinSCP fails deleting the subfolder. Had the SFTP server returned the "hidden" file in the listing (what most SFTP servers would do), WinSCP would skip it as instructed by the exclude mask, and won't try to delete the subfolder.

If you want to move all files form the server, keeping the directory structure, use this script (as you know already):
https://winscp.net/eng/docs/library_example_recursive_download_custom_error_handling
Just add the third remove = true argument to the GetFiles.
Alcasczar
[View user's profile]

Joined: 2016-07-30
Posts: 7
Location: NY
Thank you Martin

I was able to discuss the issue with our FTP admins and they setup the account so it can see the .no_delete file and the script runs perfectly without error now.

Cheers.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License