如何使用 WinSCP 上传目录中的最后 3 个文件?

How do I upload the last 3 files in a directory with WinSCP?

正在尝试上传目录中的最后 3 个文件。我想我可以设置最后 3 个文件并将它们作为别名,但我不确定该怎么做..

原来WinSCP是传输三个特定的文件名,但每天都会变。

"put apple" `
"put pear" `
"put orange" `
& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
  /log="C:\writable\path\to\log\WinSCP.log" /ini=nul `
  /command `
    "open ftp...." `
    "lcd C:\directorywithfiles" `
    "cd /" `
    # edit this to put the last 3 files
    $dir = "C:\directorywithfiles"
    $lastest = Get-ChildItem -Path "$path\*" -Exclude "*.php" -File | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 3
    $latest.name

    #this section is what i'm trying to upload
    "put <$file1>" `
    "put <$file2>" `
    "put <$file3>" `

    "exit"

$winscpResult = $LastExitCode
if ($winscpResult -eq 0)
{
  Write-Host "Success"
}
else
{
  Write-Host "Error"
}

exit $winscpResult

首先,您需要在 WinSCP 命令行之前收集名称,而不是破坏它。然后使用类似 $($lastest[0]) 的语法来引用找到的名称:

$dir = "C:\directorywithfiles"
$lastest = Get-ChildItem -Path "$path\*" -Exclude "*.php" -File |
    Sort-Object -Property LastWriteTime -Descending | Select-Object -First 3

& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
  /log="C:\writable\path\to\log\WinSCP.log" /ini=nul `
  /command `
    "open ftp...." `
    "lcd $dir" `
    "cd /" `
    "put $($lastest[0])" `
    "put $($lastest[1])" `
    "put $($lastest[2])" `
    "exit"
  • 请注意,我正在重复使用 $dir 变量。
  • 我不确定 Select-Object -First 3 是否有用。它很可能会被删除。