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:
"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"
但仅此而已下载文件。我怎样才能得到它rename and move原始文件,同时将它们保留在 FTP 站点上?
WinSCP没有类似的功能-rename
and -move
开关。因此,在简单的脚本中实现此任务并不容易,以便其以事务方式运行(仅重命名和移动已成功下载的文件)
但你可以使用WinSCP .NET 程序集 from a PowerShell 脚本.
# 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.GetFilesToDirectory(
"/USERNAME/logs", "C:\PccDataRelay\LogDownloads", "*.sqb")
# Process source files
foreach ($transfer in $transferResult.Transfers)
{
$oldName = $transfer.FileName
# 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($oldName)
Write-Host (
"Download of $oldName succeeded, moving to backup $newName")
$session.MoveFile($oldName, $newName)
}
else
{
Write-Host (
"Download of $oldName failed: $($transfer.Error.Message)")
}
}
该答案所基于的一些参考资料:
-
上传成功后将本地文件移至其他位置;
-
在批处理文件中格式化时间戳.
也可以看看:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)