GetFileToDirectory throws DirectoryNotFoundException

Advertisement

icoryx
Joined:
Posts:
1

GetFileToDirectory throws DirectoryNotFoundException

I am trying to copy a specific file from an SFTP server to my PC using Session.GetFileToDirectory in PowerShell. According to the documentation the method creates the target directory if it doesn't exist. However when I try to copy the file to a directory that doesn't exist I get an error DirectoryNotFound instead of it creating the directory.

Here's my stripped down code:
Add-Type -Path "Path\to\the\assembly.dll"
 
$password = "xxxxxx"
$secpw = ConvertTo-SecureString -String $password -AsPlainText -Force 
$fingerprint = "xxxx xxx xxxxx"
 
$sessionoptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        Hostname = "xxx.xxx.xxx.xxx"
        Username = "xxxxx"
        SecurePassword = $secpw
        SshHostKeyFingerprint = $fingerprint
        PortNumber = xxxxx
    }
$session = New-Object WinSCP.Session
$session.Open($sessionoptions)
 
$session.GetFileToDirectory("/remote/path/to/file.zip", "C:\temp\folderthatdoesnotexist\", $false, $null)
It wouldn't be hard to just create the directory first but since the documentation says that the method already does that I'm a bit confused why it's not working. Is it a permissions problem?

Reply with quote

Advertisement

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

Re: GetFileToDirectory throws DirectoryNotFoundException

Indeed, the directory is NOT created. It was a mistake in the documentation. I've corrected it. Thanks.

Reply with quote

Advertisement

You can post new topics in this forum