Topic "Powershell Parallel Connections Script Bug with Single File Download"

Author Message
vanlee1987
[View user's profile]

Joined: 2016-06-27
Posts: 2
Location: Arizona
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:
Code:
$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):

Code:
# 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).
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
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
[View user's profile]

Joined: 2016-06-27
Posts: 2
Location: Arizona
Hello Martin, thank you for the reply, big fan of the application. So, if I add some criteria to the file selection like so:

Code:
# 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:

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


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

Code:
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
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
OK, I understand now.

I've used this solution:

[array]$files = ...

As per: http://stackoverflow.com/a/11107477/850848
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License