免责声明 #1:我在 WinSCP 的 forum 上发布了这个问题,但没有收到任何回复,而且这个问题是时间敏感的。 免责声明 #2:我将这个交叉帖子的合法性基于 advice 从 Meta 的主题中找到.有了那个序言......

到目前为止,我一直在使用 WS_FTP,但想切换到 WinSCP。
我正在尝试将此 WS_FTP 脚本转换为 WinSCP:

"C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe"
-s "sftp://USERNAME:[email protected]/USERNAME/logs/*.sqb"
-d "local:C:\PccDataRelay\LogDownloads\"
-rename tx_[yyyy]-[mm]-[dd]_[hh]-[tt]-[ss]_[OnlyName].[OnlyExt]
-move "/USERNAME/logs/transferred/"

我已经能够部分转换它:
cd C:\PccDataRelay\TestDownloads

"C:\Program Files (x86)\WinSCP\winscp.com" /ini=nul /log=C:\PccDataRelay\AuditLogs\incremental_download.log /command ^
    "open sftp://USERNAME:[email protected]/USERNAME/logs/ -hostkey=""ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx""" ^
    "get /USERNAME/logs/*.sqb" ^
    "exit"

但这只会下载文件。如何让它重命名和移动原始文件,同时将它们留在 FTP 站点上?

最佳答案

WinSCP 没有类似于 -rename-move 开关的功能。因此,在简单的脚本中实现此任务并不容易,因此它具有事务性的行为(仅重命名和移动已成功下载的文件)

但是您可以使用 WinSCP .NET assembly 中的 PowerShell script

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "ftpus.pointclickcare.com"
    UserName = "USERNAME"
    Password = "PASSWORD"
    SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
}

# Connect
Write-Host "Connecting..."
$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\PccDataRelay\AuditLogs\incremental_download.log"
$session.Open($sessionOptions)

# Download files
$transferResult =
     $session.GetFiles("/USERNAME/logs/*.sqb", "C:\PccDataRelay\LogDownloads\*")

# Process source files
foreach ($transfer in $transferResult.Transfers)
{
    # Success or error?
    if ($transfer.Error -eq $Null)
    {
        $newName =
            "/USERNAME/logs/transferred/tx_" +
            (Get-Date -Format "yyyy-MM-dd-hh-mm-ss") + "_" +
            [IO.Path]::GetFileName($transfer.FileName)
        Write-Host (
            "Download of $($transfer.FileName) succeeded, moving to backup $newName")
        $session.MoveFile($transfer.FileName, $newName)
    }
    else
    {
        Write-Host (
            "Download of $($transfer.FileName) failed: $($transfer.Error.Message)")
    }
}

此答案基于的一些引用资料:
  • Moving local files to different location after successful upload ;
  • Formatting timestamp in batch file

  • 也可以看看:
  • Converting WinSCP script to code based on .NET assembly
  • 关于ftp - 用于下载、重命名和移动文件的 WinSCP 脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48311614/

    10-12 19:59