如何使用 WinSCP 下载较新的文件并将它们保存到两个本地文件夹?
How to download newer files using WinSCP and save them to two local folders?
我的文件夹 (ftp_region
) 中有一个文件,其内容与我 FTP 中的数据相似。
如何从我的 FTP 下载较新的文件并将它们保存到两个本地文件夹(ftp_region
和其他文件夹)
现在,我使用这个:
open "my ftp address"
get -neweronly "..." "..."
您不能使用普通的 WinSCP 脚本来执行此操作。
你最好使用WinSCP .NET assembly。它允许您处理下载的文件。
PowerShell中的示例代码:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.example.com"
UserName = "user"
Password = "password"
}
Write-Host "Connecting..."
$session = New-Object WinSCP.Session
# $session.SessionLogPath = "C:\path\to\log\sync.log"
$session.Open($sessionOptions)
$remotePath = "/remote/path"
$localPathPrimary = "C:\backup\primary"
$localPathSecondary = "C:\backup\secondary"
Write-Host "Synchronizing..."
$result =
$session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPathPrimary, $remotePath,
$False)
$result.Check()
Write-Host "Copying downloaded files to secondary backup..."
foreach ($download in $result.Downloads)
{
$filename = (Split-Path -Leaf $download.Destination)
$localFilePathSecondary = (Join-Path $localPathSecondary $filename)
Copy-Item $download.Destination $localFilePathSecondary
Write-Host "File $filename archived to both folders."
}
代码基于官方示例Deleting remote files after successful remote to local synchronization。
我的文件夹 (ftp_region
) 中有一个文件,其内容与我 FTP 中的数据相似。
如何从我的 FTP 下载较新的文件并将它们保存到两个本地文件夹(ftp_region
和其他文件夹)
现在,我使用这个:
open "my ftp address"
get -neweronly "..." "..."
您不能使用普通的 WinSCP 脚本来执行此操作。
你最好使用WinSCP .NET assembly。它允许您处理下载的文件。
PowerShell中的示例代码:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.example.com"
UserName = "user"
Password = "password"
}
Write-Host "Connecting..."
$session = New-Object WinSCP.Session
# $session.SessionLogPath = "C:\path\to\log\sync.log"
$session.Open($sessionOptions)
$remotePath = "/remote/path"
$localPathPrimary = "C:\backup\primary"
$localPathSecondary = "C:\backup\secondary"
Write-Host "Synchronizing..."
$result =
$session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPathPrimary, $remotePath,
$False)
$result.Check()
Write-Host "Copying downloaded files to secondary backup..."
foreach ($download in $result.Downloads)
{
$filename = (Split-Path -Leaf $download.Destination)
$localFilePathSecondary = (Join-Path $localPathSecondary $filename)
Copy-Item $download.Destination $localFilePathSecondary
Write-Host "File $filename archived to both folders."
}
代码基于官方示例Deleting remote files after successful remote to local synchronization。