How to upload a file with a specific keyword in a name for SFTP using WinSCP?

Advertisement

albrechtmyers
Joined:
Posts:
1
Location:
Glenolden

How to upload a file with a specific keyword in a name for SFTP using WinSCP?

I currently have a working script that uploads files to a remote SFTP directory. The problem I am facing is that there will be 3 files and they must be loaded in sequence at different intervals. I’ve already thought about using the Windows Task Scheduler to monitor the download frequency, but there is another problem. I determined that the files differ in name based on one keyword. Is there a way to change my code to search for files in a directory by a specific name? For example, he searches the directory for a file named "client" in his name. Based on this keyword / name, it then downloads this particular file. See the current working scenario:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
 
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "server"
    UserName = "username"
    Password = "password"
    SshHostKeyFingerprint = "key"
}
 
$session = New-Object WinSCP.Session
try
{
    # Connect
    $session.Open($sessionOptions)
 
    # Upload files
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
    $transferResult =
        $session.PutFiles("E:\CMBPAID", "/NESAMSCARIMED", $False, $transferOptions)
 
    # Throw on any error
    $transferResult.Check()
 
    # Print results
    foreach ($transfer in $transferResult.Transfers)
    {
        Write-Host "Upload of $($transfer.FileName) succeeded"
    }
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum