Powershell - Download folders from ftp with wildcard
Hi all,
I'm using a Powershell script to auto download updated files from an FTP directory, im using the synchronisation method for this. I want to be able to specify a directory with a wildcard. Like
folder /download/software/version/4*. Or be able to download everything with folder /download/software/version/400 and higher. I want to do this, because there are older version, ex. version 100, 200, 300 which won't need to be downloaded, exact 400 and higher.
Is there any way to realise this? Currently using this script:
If I add a wildcard to the /Download/Software/4* directory it fails with the following error:
I'm using a Powershell script to auto download updated files from an FTP directory, im using the synchronisation method for this. I want to be able to specify a directory with a wildcard. Like
folder /download/software/version/4*. Or be able to download everything with folder /download/software/version/400 and higher. I want to do this, because there are older version, ex. version 100, 200, 300 which won't need to be downloaded, exact 400 and higher.
Is there any way to realise this? Currently using this script:
# Specify directories $localPath = "C:\Temp\Software" $remotePath = "/Download/Software/Version/" # Load WinSCP .NET assembly Add-Type -Path "C:\Program Files (x86)\WinSCP-5.13.4-Automation\WinSCPnet.dll" try { # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::ftp HostName = "download.ftp.com" UserName = "anonymous" Password = "anonymous" } $session = New-Object WinSCP.Session try { # Connect Write-Host "Connecting to FTP..." $session.Open($sessionOptions) # Download files $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary Write-Host "Synchronizing changes..." $transferResult = $session.SynchronizeDirectories( [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $false. $transferOptions) # Throw on any error $transferResult.Check() # Print results foreach ($transfer in $transferResult.Downloads) { Write-Host "Download of $($transfer.FileName) succeeded" } } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch { Write-Host "Error: $($_.Exception.Message)" exit 1 }
If I add a wildcard to the /Download/Software/4* directory it fails with the following error:
PS C:\temp> C:\Temp\DownloadSoftware.ps1
Connecting to FTP...
Synchronizing changes...
Error: Exception calling "Check" with "0" argument(s): "Error listing directory '/Download/Software/4*'.
Could not retrieve directory listing
The filename, directory name, or volume label syntax is incorrect. "