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

时间:2018-08-31 18:40:52

标签: powershell sftp winscp winscp-net

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

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

1 个答案:

答案 0 :(得分:2)

使用file mask *customer*

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

(请注意在目标路径末尾添加了斜杠)