Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

Change script to include only particular files.

Hi Experts,

The script below connects to a FTP site and downloads all newest files to a local folder in my pc.

How can I change this script to include only files  either with wording "PAT" or "Sched"?
However if file name contains word "Full" it should not be included.

# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "sftp.MySite.com"
    UserName = "MyUserName"
    Password = "MyPWD"
    SshHostKeyFingerprint = "1234567890="
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Transfer files
    $remotePath = "H:\FTP\*"
    
    $sourcePath = "/Outbox/*"
    $destPath = "H:\FTP\"
    $destPathNew = "H:\FTP\Caspio\"


    $transferOptions = New-Object WinSCP.TransferOptions
    while($True)
    {
 
        try
        {
     
            $transferResult = $session.GetFiles($sourcePath, $destPath, $False, $transferOptions)
            $transferResult.Check()
        }
        finally
        {
 	        foreach ($transfer in $transferResult.Transfers)
                {
                   $session.GetFiles($transfer.FileName, $destPathNew, $False, $transferOptions)
          
                    Write-Host "Download of $($transfer.FileName) succeeded"
                }
                $destPathNew1
        }
    }
    Write-Host "Waiting..."
    Start-Sleep -Seconds 5

}
finally
{

    $session.Dispose()
}

Open in new window


Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Member_2_6404472
Member_2_6404472

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bfuchs

ASKER

Thank you!
Avatar of Member_2_6404472
Member_2_6404472

You are welcome