Download multiple files from an array

Advertisement

tkennedy
Joined:
Posts:
11

Download multiple files from an array

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#net 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:
$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.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: Download multiple files from an array

I do not see a reason why this should not work.
Please set Session.DebugLogPath and attach the log.

Reply with quote

tkennedy
Joined:
Posts:
11

Re: Download multiple files from an array

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.

Reply with quote

tkennedy
Joined:
Posts:
11

Re: Download multiple files from an array

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

Reply with quote

Advertisement

You can post new topics in this forum