Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

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: Session.GetFileToDirectory Not creating local directory if it doesn't exist

Thanks for your report. Indeed, the documentation is wrong. I've removed that statement.
Tegy

Session.GetFileToDirectory Not creating local directory if it doesn't exist

I'm trying to pull files to my local machine like so:
foreach ($fileInfo in $Files){
    Write-Host "Downloading $($fileInfo.FullName) ..."
    $filePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
    $FileFullNameBS = $fileInfo.FullName.Replace("/", "\")
    $sp = Split-Path -Path $FileFullNameBS
    $LocalDirectory = "$($localPath)$($sp)"
    $session.GetFileToDirectory($filePath, $LocalDirectory, $false)
}

Everything works fine if I already have the directory created on my local machine but if it doesn't exist, I get:

Exception calling "GetFileToDirectory" with "3" argument(s): Local Directory
At localfilepath\txtmerge.ps1:69 char:5
+ $session.GetFileToDirectory($filePath, $LocalDirectory, $false)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DirectoryNotFoundException

I might be wrong as I am fairly new to PowerShell but It says on the documentation
If the target local directory does not exist, it is created.

I must be missing something. Thanks in advance for any light shed on this.