如何使用 WinSCP 将名称中带有特定关键字的文件上传到 SFTP

How do I upload file with specific keyword in name to SFTP with WinSCP

我目前有一个可以将文件上传到 SFTP 远程目录的工作脚本。我遇到的问题是将有 3 个文件,它们应该以不同的时间间隔按顺序上传。我已经考虑过使用 Windows Task Scheduler 来处理上传频率,但还有另一个问题。我已经确定这些文件在基于一个关键字的命名上有所不同。有没有一种方法可以修改我的代码以按特定名称查找目录中的文件?例如,它在目录中搜索名称中带有 "customer" 的文件。然后基于 keyword/name 上传该特定文件。请查看当前工作脚本:

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

使用file mask *customer*:

$transferResult =
    $session.PutFiles("E:\CMBPAID\*customer*", "/NESAMSCARIMED/", $False, $transferOptions)

(注意目标路径末尾加的斜线)