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: GetFileToDirectory throws DirectoryNotFoundException

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

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?