winscp powershell getfiles/getfile修改远程文件时间戳
我正在尝试编写PowerShell WinSCP脚本,以从SFTP服务器中摘取最近修改的远程文件。 WINSCP在他们的文档中有一个不错的例子。
我真的很挣扎,因为会话正在将远程文件时间戳修改为当前日期。我什至尝试过枚举台词,并为每个文件做一个getfile,并且正在做同样的事情。奇怪的是,它在远程服务器上的所有文件上都可以做到这一点。但是它破坏了脚本的整个目的,因为它最终只是一遍又一遍地下载相同的文件,因为the LastwriteTime在远程文件上被更新。
在远程FTP服务器上是否有一些设置,当访问文件时会更新时间戳?我的同事最近手动传输文件,他们都保留了时间戳,所以如果某种FTP设置仅影响我的脚本,这会让我感到困惑。
我尝试过WINSCP 5.19.6和5.20 beta。仍然存在同一问题。
我将非常感谢任何见解。
脚本如下:
# Set the last timestamp if exists
$TransferOptions = New-Object WinSCP.TransferOptions
if ($LastTimestamp -ne $Null) {
Write-Host "Downloading files modified after $LastTimestamp..."
$FileMask = "*>" + $LastTimestamp.ToString("yyyy-MM-dd HH:mm:ss")
Write-Host "FileMask: $FileMask"
$TransferOptions.FileMask = ($FileMask)
} else {
Write-Host "No timestamp found, downloading all files"
$FileMask = "| Aaaaa/; Bbbbbb/"
Write-Host "FileMask: $FileMask"
$TransferOptions.FileMask = ($FileMask)
}
# Perform the transfer
try {
$TransferResult = $Session.GetFiles("/", $LocalPath, $False, $TransferOptions)
$TransferResult.Check()
$LatestTransfer =
$TransferResult.Transfers |
Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } } `
-Descending |
Select-Object -First 1
if ($LatestTransfer -eq $Null)
{
Write-Host "No newer files found."
}
else
{
$LastTimestamp = (Get-Item $latestTransfer.Destination).LastWriteTime
Write-Host (
"Downloaded $($transferResult.Transfers.Count) files, " +
"latest being $($latestTransfer.FileName) with timestamp $lastTimestamp.")
}
} catch {
Write-Error "Error downloading files, exiting without saving timestamp:"
Write-Error $_
exit
}
# Save the last timestamp
Set-Content -Path $TimestampFile -Value $LastTimestamp.ToString("O")
I'm trying to write a PowerShell WinSCP script to pull recently modified remote files from an SFTP server. WinSCP has a decent example in their documentation.
I'm really struggling though as Session.GetFiles is modifying the remote files timestamp to the current date. I've even tried EnumerateDirectory and doing a single GetFile for each file and it's doing the same thing. Oddly enough it does it on all the files on the remote server except a couple of them. But it defeats the entire purpose of the script because it ends up just downloading the same files over and over again since the lastwritetime gets updated on the remote file.
Is there some setting on the remote FTP server that when the file is accessed it updates the timestamp? My coworker was manually transferring the files recently and they all had their timestamps preserved so it would confound me if some kind of FTP setting was only affecting my script.
I've tried WinSCP 5.19.6 and 5.20 beta. Still the same issue.
I would greatly appreciate any insight.
Script is below:
# Set the last timestamp if exists
$TransferOptions = New-Object WinSCP.TransferOptions
if ($LastTimestamp -ne $Null) {
Write-Host "Downloading files modified after $LastTimestamp..."
$FileMask = "*>" + $LastTimestamp.ToString("yyyy-MM-dd HH:mm:ss")
Write-Host "FileMask: $FileMask"
$TransferOptions.FileMask = ($FileMask)
} else {
Write-Host "No timestamp found, downloading all files"
$FileMask = "| Aaaaa/; Bbbbbb/"
Write-Host "FileMask: $FileMask"
$TransferOptions.FileMask = ($FileMask)
}
# Perform the transfer
try {
$TransferResult = $Session.GetFiles("/", $LocalPath, $False, $TransferOptions)
$TransferResult.Check()
$LatestTransfer =
$TransferResult.Transfers |
Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } } `
-Descending |
Select-Object -First 1
if ($LatestTransfer -eq $Null)
{
Write-Host "No newer files found."
}
else
{
$LastTimestamp = (Get-Item $latestTransfer.Destination).LastWriteTime
Write-Host (
"Downloaded $($transferResult.Transfers.Count) files, " +
"latest being $($latestTransfer.FileName) with timestamp $lastTimestamp.")
}
} catch {
Write-Error "Error downloading files, exiting without saving timestamp:"
Write-Error $_
exit
}
# Save the last timestamp
Set-Content -Path $TimestampFile -Value $LastTimestamp.ToString("O")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题。
设置TransferOptions.preservetimestamp false解决问题,这很奇怪,因为它应该只保留到本地下载的文件的时间戳。
“保存时间戳(将目标文件的最后写入时间设置为源文件的最后一个写入时间)。默认为true。”
I've somewhat solved the issue.
Setting TransferOptions.PreserveTimeStamp to False resolves the issue, which is odd because it's supposed to just reserve the timestamp to the locally downloaded files.
"Preserve timestamp (set last write time of destination file to that of source file). Defaults to true."