Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Error in PutFileToDirectory() - How to Get Error Code?

There's no "error code" property. There are no set error codes in WinSCP. This is SFTP only error code. Other protocols have completely different codes.

Your related question:
SessionRemoteException class questions
aksarben

Error in PutFileToDirectory() - How to Get Error Code?

I encountered an error calling PutFileToDirectory():
try {
    $hostDirectory = Get-PathBelowWebRoot -pcRoot $pcRoot -file $file
    $remove = $false
    $session.PutFileToDirectory($file.fullName, $hostDirectory, $remove, $options)
}
catch {
    Write-Error $_
    $($_.InvocationInfo)
    exit
}

PowerShell issues the following error message:

Write-Error: Exception calling "PutFileToDirectory" with "4" argument(s): "Cannot create remote file '/C:/Users/Dick/\bib/bibliography.htm'. No such file or directory. Error code: 2 Error message from server: No such file"

This error is not unexpected, since often the target folder won't exist yet. But, how do I find the error code returned from the call? (apparently "2" in this case).

Do I have to parse error messages? I didn't see an “error code” property in the thrown exception. If the issue is simply that the target directory doesn't exist, I can create the directory & retry. But if the problem is something else, I need to know to figure out what action to take.

$PSVersionTable.Version:
Major  Minor  Patch  PreReleaseLabel BuildLabel

-----  -----  -----  --------------- ----------
7      3      4

Using the PowerShell extension to Visual Studio Code 1.79.0.