Topic "Download multiple files from an array"

Author Message
tkennedy
[View user's profile]

Joined: 2012-10-26
Posts: 11
I'm using powershell to script the download of multiple files. The file names are held in an array and I use a foreach loop to loop through the array. This works great as long as each file in the array is on the server, but if one of the files is not available, none of the successive files in the array are downloaded. I read https://winscp.net/eng/docs/scriptcommand_get#converting_to_.net_assembly and it states that I need to call GetFiles for each file I'm trying to download. Can you please help me with my logic on this? Here is my code:

Code:
$GetFileMask = @("Weekly_Values*.pgp", "Monthly_Values*.pgp")

# Download the files according to the file mask
foreach ($file in $GetFileMask) {

         # Download the file and throw on any error
         $transferResult = $Session.GetFiles("$ServerPath$file", "$DLPATH$file")
         $transferResult.Check();
}


Any help on this would be greatly appreciated. This is driving me nuts.
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25015
Location: Prague, Czechia
I do not see a reason why this should not work.
Please set Session.DebugLogPath and attach the log.
_________________
Martin Prikryl
tkennedy
[View user's profile]

Joined: 2012-10-26
Posts: 11
prikryl wrote:
I do not see a reason why this should not work.
Please set Session.DebugLogPath and attach the log.


Attached is a session log. If both files exist on the server, then they are both downloaded. If the "Weekly_Values" file exists only (the first in the array), it is downloaded if only the "Monthly_Values" file exists, nothing is downloaded.
A5B10-05062013-Session.log (8.11 KB) [Download]

Description: (none)

tkennedy
[View user's profile]

Joined: 2012-10-26
Posts: 11
After digging further it looks like the problem is further down in my code. I am trying to do a file rename using the wildcard name which is giving an error. The error drops to a catch/finally block and exits the script.

Code:
if ($RenameOnServer -eq $true) {
     # If the file was downloaded successfully rename it but leave it on the server
     $Session.MoveFile($FullDLFile, "$FullDLFile$RenameExtension")                       
}


Since I'm using a wildcard, I do not know the exact name of the file. Is there a way to grab the file name of the file I just downloaded?
tkennedy
[View user's profile]

Joined: 2012-10-26
Posts: 11
tkennedy wrote:
After digging further it looks like the problem is further down in my code. I am trying to do a file rename using the wildcard name which is giving an error. The error drops to a catch/finally block and exits the script.

Code:
if ($RenameOnServer -eq $true) {
     # If the file was downloaded successfully rename it but leave it on the server
     $Session.MoveFile($FullDLFile, "$FullDLFile$RenameExtension")                       
}


Since I'm using a wildcard, I do not know the exact name of the file. Is there a way to grab the file name of the file I just downloaded?


I re-wrote the function and was able to get it to work. Thanks Martin!
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