Optimizing a large script for FTP File downloads (~1100 lines)
$session.Timeout = New-TimeSpan -Seconds 600
I have a large FTP structure and I need certain files from certain subfolders. I have broken it up into many blocks like this, all with different filemasks and root/local folder parameters:
# Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Set up session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Ftp HostName = "x" UserName = "x" Password = "x" FtpSecure = [WinSCP.FtpSecure]::Implicit } $session = New-Object WinSCP.Session $remotePath = "/FTPFolder1/Subfolder1/Subf2/" $localPath = "C:\Users\ABBBB\xxxxxx\xxxxxxx\xxxxxxx\xxxxxx\xxxxxxxx\" try { # Connect $session.Open($sessionOptions) $session.Timeout = New-TimeSpan -Seconds 600 # Masking $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.FileMask = "Census Data Files/*/*.xlsx" # Download files $transferOptions.AddRawSettings("ExcludeEmptyDirectories", "1") $session.GetFiles($remotePath, $localPath, $False, $transferOptions).Check() } finally { # Disconnect, clean up $session.Dispose() }