Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

albrechtmyers

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()
}