WinSCP脚本,用于下载,重命名和移动文件 [英] A WinSCP script to download, rename, and move files

查看:784
本文介绍了WinSCP脚本,用于下载,重命名和移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明#1:我在WinSCP的论坛,但未收到任何回复,并且此问题是时间敏感的.
免责声明#2:我将此交叉发布的合法性基于

Disclaimer #1: I posted this question on the WinSCP's forum but haven't received any replies and this issue is time-sensitive.
Disclaimer #2: I'm basing the legitimacy of this cross-post on advice found on the subject from Meta.
With that preamble...

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

Up until now, I have been been using WS_FTP but would like to switch to WinSCP. I'm trying to convert this WS_FTP script into WinSCP:

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

我已经能够对其进行部分转换:

I've been able to convert it partially:

cd C:\PccDataRelay\TestDownloads

"C:\Program Files (x86)\WinSCP\winscp.com" /ini=nul /log=C:\PccDataRelay\AuditLogs\incremental_download.log /command ^
    "open sftp://USERNAME:PASSWORD@ftpus.pointclickcare.com/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站点上呢?

But that only downloads the files. How can I get it to rename and move the original files while also leaving them in-place on the FTP site?

推荐答案

WinSCP不具有与-rename-move开关相似的功能.因此,以简单的脚本来实现此任务以使其具有事务性行为(重命名并仅移动成功下载的文件)是不容易的

WinSCP does not have a feature similar to -rename and -move switches. So it's not easy to implement this task in a simple scripting so that it behaves transactionally (renames and moves only the files that were successfully downloaded)

但是您可以从 WinSCP .NET程序集使用://winscp.net/eng/docs/library_powershell"rel =" nofollow noreferrer> PowerShell脚本.

But you can use WinSCP .NET assembly from a 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)")
    }
}

有些人引用此答案基于:

Some references that this answer is based on:

  • Moving local files to different location after successful upload;
  • Formatting timestamp in batch file.

另请参阅:

这篇关于WinSCP脚本,用于下载,重命名和移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆