4

I am using a PowerShell script using the module WinSCP to download files from a SFTP server.

I am using a file mask so I only download files matching *.zip which have been added today. The SFTP folder structure is as follows:

\folderA\
\folderA\folderB

What should happen is that any files matching the file mask will be downloaded from folderA to C:\Files. But what is happening is when the matching files are downloaded, folderB is also created in C:\Files which I do not want. I could add a delete line to remove it but I will be adding to the script to download files from folderB to the same location, C:\Files so I cannot workout how to just download matching files without the corresponding folder structure being created.

Here is the code being used:

$ftpConfig = @{
    HostName     = "ftp.server.com"
    UserName     = "user"
    RemotePath   = "/foldera/"
    LocalPath    = "C:\Files\"    
}

$transferOptions.FileMask = "*.zip>Today"
Receive-WinSCPItem -WinSCPSession $winscpSession -TransferOptions $transferOptions -RemotePath $ftpConfig.RemotePath -LocalPath $ftpConfig.LocalPath

Note that I have excluded the contents for $winscpSession as I do not believe its relevant to the outcome.

I have tried setting RemotePath to the following:

/remote - This will download the whole folder structure so the result will be C:\Files\folderA\folderB. Plus any files matching will appear in C:\Files\folderA\.

/remote* - This does not download any files even though there are which match. /remote/* - Same result as /remote*, nothing gets downloaded.

None of these do not give me the desired outcome.

Any suggestions?
Thank you!

1 Answer 1

2

If you want to avoid downloading any subfolders, exclude them in the filemask:

$transferOptions.FileMask = "*.zip>Today | */"

https://winscp.net/eng/docs/faq_script_non_recursive
https://winscp.net/eng/docs/file_mask


If you wan to avoid downloading/creating only subfolders that do not contain any matching files, use ExcludeEmptyDirectories raw transfer settings:

$transferOptions.AddRawSettings("ExcludeEmptyDirectories", "on")

https://winscp.net/eng/docs/rawtransfersettings#excludeemptydirectories

Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm, amazing! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.