Powershell Parallel Connections Script Bug with Single File Download

Advertisement

vanlee1987
Joined:
Posts:
2
Location:
Arizona

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).

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

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.

Reply with quote

vanlee1987
Joined:
Posts:
2
Location:
Arizona

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!

Reply with quote

Advertisement

You can post new topics in this forum