Error when trying to delete file from remote site

Advertisement

eggi214
Joined:
Posts:
1
Location:
Manchester, UK

Error when trying to delete file from remote site

Hi there,

I've created a powershell script to upload and download a file from a remote FTPS servier.

The script does the following:

1. Uploads a file from my local dir
2. Moves the uploaded file into a local backup dir
3. Download files from the remote FTPS server to local dir
4. Remove downloaded file from the remote site.

I'm having issues with point 4. I can manually delete/upload files to that folder I'm trying to download from, but I get the following error when running the script.

Download of /outbound/tes.tst /outbound/* succeeded, deleting from remote
Error: Exception calling "Check" with "0" argument(s): "Can't get attributes of file '/outbound/tes.tst'.

File or folder '/outbound/tes.tst' does not exist."

My Script is


# Load WinSCP .NET assembly
Add-Type -Path "c:\Program Files (x86)\WinSCP\WinSCPnet.dll"


$localAckPath = "C:\Users\user\Documents\test files\ACK\"
$remoteAckPath = "/outbound/test/*"
$backupOrdPath = "C:\Users\user\Documents\test files\localbackup\"
$remoteOrdPath = "/inbound/test/"
$localOrdFilePath = "C:\Users\user\Documents\test files\file to upload\*"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Timeout = new-timespan -minutes 1
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "123.122.250.123"
    PortNumber = port
    UserName = "user"
    Password = "pass"
    FtpSecure = [WinSCP.FtpSecure]::Implicit
    TlsHostCertificateFingerprint = "fingerprint"
    
}


while($true)
{

$session = New-Object WinSCP.Session


try
{

   
    Write-Host "Checking..."
    
    # Connect
    $session.Open($sessionOptions)
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
   $transferOptions.PreserveTimestamp = $False
   
   $transferResult =
            $session.PutFiles( $localOrdFilePath, "/inbound/", $False, $transferOptions)

            foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupOrdPath
            }
            else
            {
                Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
            }
        }


        $transferResult =
            $session.GetFiles($remoteAckPath, $localAckPath, $False, $transferOptions)

            foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                Write-Host "Download of $($transfer.FileName) $($remoteAckPath) succeeded, deleting from remote"

            $filename = [WinSCP.RemotePath]::EscapeFileMask($transfer.FileName)
                $remoteAckPath = $session.RemoveFiles($filename)
            $remoteAckPath.Check()
                
            if ($remoteAckPath.IsSuccess)
                {
                    Write-Host "Removing of file $($transfer.FileName) succeeded"
                }
                else
                {
                    Write-Host "Removing of file $($transfer.FileName) failed"
                }
                # Upload succeeded, move source file to backup
                #Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host "Download of $($transfer.FileName) failed: $($transfer.Error.Message)"
            }
        }



    # Your code
 
        # Throw on any error
       # $transferResult.Check()
 
        # Print results
       # foreach ($transfer in $transferResult.Transfers)
        #{
         #   Write-Host "Upload of $($transfer.FileName) succeeded"
        #}
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    #exit 1
}

finally
{
    $session.Dispose()
}
#exit 0
   Write-Host "Sleeping..."
   Start-Sleep -Seconds 300  


}

[/code]

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
28,802
Location:
Prague, Czechia

Re: Error when trying to delete file from remote site

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.

Btw, doesn't the FTP server automatically delete the files, once they are downloaded?
Did you test the same in WinSCP GUI (download and delete afterwards)? Can you post a log file for that?

Reply with quote

Advertisement

You can post new topics in this forum