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

vanlee1987

Re: Powershell Parallel Connections Script Bug with Single File Download

Hello Martin, thank you for the reply, big fan of the application. So, if I add some criteria to the file selection like so:

# Retrieve list of files and sort them from largest to smallest

$files =
   $session.ListDirectory($remotePath).Files |
   Where-Object { -Not $_.IsDirectory } |
   Where-Object { $_.Name -Like "File1*" } | #Limiting Criteria
   Sort-Object Length -Descending


I might end up with only one file that I want out of many in the directory. But when I generate the filelist:

$fileList = $files[$start..$i] -join ";"


I end up with a blank variable. So when I hit here:

foreach ($file in $files)

{
     $remoteFilePath = "$remotePath/$file"
     $localFilePath = "$localPath\$file"
     Write-Host "Downloading $remoteFilePath to $localFilePath in $no"
 
     $session.GetFiles($session.EscapeFileMask($remoteFilePath), $localFilePath).Check()
}


I end up downloading the whole $remotePath directory.

Thank you for your continued support of this app!
martin

Re: Powershell Parallel Connections Script Bug with Single File Download

Thanks for your feedback.

Though I'm not sure that you exactly mean by "It's only a bug if you are filtering the file list in a directory.". How are you filtering the files list? Can you give an example? I was not able to reproduce the problem you are describing.
vanlee1987

Powershell Parallel Connections Script Bug with Single File Download

When using the library example for parallel transfers in Powershell, if only one file is found, the entire directory is downloaded. This is not ideal when filtering the direct based on some criteria (IE, lastmodifieddate, wildcard, etc).

Here is the script: https://winscp.net/eng/docs/library_example_parallel_transfers

The issue is at line 54, this results in an empty variable within the loop when only one item exists:
$fileList = $files[$start..$i] -join ";"


I implemented a fix in my code by converting $files into an array after the list is generated further up in the code (line 31):

# If single file add to array

if ($files.Count -eq 1){
   $files = @($files)
}


There is probably a more elegant way of handling this. It's only a bug if you are filtering the file list in a directory. Otherwise only one file exists in the directory, so the result is one file being download (via the whole directory).