简体   繁体   English

.NET WinSCP,仅下载新文件

[英].NET WinSCP, only download new files

I've created a program that's supposed to run once each night. 我创建了一个程序,该程序应该每晚运行一次。 What it does is that it downloads images from my FTP, compresses them and uploads them back to the FTP. 它的作用是从我的FTP下载图像,对其进行压缩并将其上传回FTP。 I'm using WinSCP for downloading and uploading files. 我正在使用WinSCP来下载和上传文件。

Right now I have a filemask applied that makes sure that only images are downloaded, that subdirectories are excluded and most importantly that only files that are modified the last 24 hours are downloaded. 现在,我应用了文件掩码,以确保仅下载图像,排除子目录,最重要的是,仅下载最近24小时修改的文件。 Code snippet for this filemask: 此文件掩码的代码段:

DateTime currentDate = DateTime.Now;
string date = currentDate.AddHours(-24).ToString("yyyy-MM-dd");

transferOptions.FileMask = "*.jpg>=" + date + "; *.png>=" + date + "|*/";

Thing is, as I'm about to publish this I realize that if I run this once per night, and it checks if files are modified the last 24 hours, it will just keep downloading and compressing the same files, as the modified timestamp will keep increasing for each compression. 事实是,当我要发布此文件时,我意识到如果我每晚运行一次,并且它会检查文件是否在最近24小时内被修改,它将继续下载和压缩相同的文件,因为修改后的时间戳会每次压缩都保持增加。

To fix this I need to edit the FileMask to only download NEW files, ie files that weren't in the folder the last time the program was run. 为了解决这个问题,我需要编辑FileMask以仅下载新文件,即上次运行程序时不在文件夹中的文件。 I don't know if you can check the Created By-timestamp in some way, or if I have to do some comparisons. 我不知道您是否可以通过某种方式检查“创建的时间戳”,或者是否必须进行一些比较。 I've been looking through the docs but I haven't found any solution to my specific use case. 我一直在浏览文档,但没有找到针对我的特定用例的任何解决方案。

Is there anyone experienced in WinSCP that can point me in the right direction? 有没有WinSCP经验丰富的人员可以为我指明正确的方向?

It doesn't look like WinSCP can access the Created Date of the files. WinSCP似乎无法访问文件的创建日期。

Unless you can do something to make the files 'different' when you re-upload them (eg put them in a different folder) then you best option might be: 除非在重新上传文件时可以做一些事情来使文件“不同”(例如,将它们放在另一个文件夹中),否则最佳选择可能是:

  • Forget about using FileMask 忘记使用FileMask
  • Use WinSCP method EnumerateRemoteFiles to get a list of the files 使用WinSCP方法EnumerateRemoteFiles获取文件列表
  • Loop through them yourself (its a collection of RemoteFileInfo objects 自己遍历它们(它是RemoteFileInfo对象的集合
  • You'll probably need to keep a list of 'files already processed' somewhere and compare with that list 您可能需要在某处保留“已处理文件”列表,然后与该列表进行比较
  • Call GetFiles for the specific files that you actually want 为您实际需要的特定文件调用GetFiles

There's a whole article on WinSCP site on How do I transfer new/modified files only? 在WinSCP网站上有整篇文章,关于如何仅传输新文件/修改过的文件?

To summarize the article: 总结文章:

  • If you keep the past files locally, just run synchronization to download only the modified/new ones. 如果您将过去的文件保存在本地,则只需运行同步即可仅下载已修改/新的文件。

    Then iterate the list returned by Session.SynchronizeDirectories to find out what the new files are. 然后迭代Session.SynchronizeDirectories返回的列表,以查找新文件。

  • Otherwise you have to use a time threshold. 否则,您必须使用时间阈值。 Just remember the last time you ran your application and use a time constraint that includes also a time, not just a date. 只需记住上一次运行应用程序并使用时间约束,其中还包括一个时间,而不仅仅是一个日期。

     string date = lastRun.ToString("yyyy-MM-dd HH:mm:ss"); transferOptions.FileMask = "*.jpg>=" + date + "; *.png>=" + date + "|*/"; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SSH 主机密钥指纹...与模式不匹配...在 C# 中使用 WinSCP .NET 程序集下载文件时 - SSH host key fingerprint ... does not match pattern ... when using WinSCP .NET assembly in C# to download files 使用 WinSCP .NET 程序集从 FTP 服务器下载除特定文件夹之外的所有文件 - Download all files except a specific folder from FTP server using WinSCP .NET assembly WinSCP .NET程序集:如何下载目录 - WinSCP .NET assembly: How to download directories 使用WinSCP .NET程序集仅使用SFTP协议删除或移动所选文件 - Remove or move only selected files with SFTP protocol using WinSCP .NET assembly WinSCP .NET程序集-如何将文件上传到主目录? - WinSCP .NET assembly - How to upload files to home? 使用 WinSCP .NET 程序集上传后删除文件 - Delete files after upload with WinSCP .NET assembly 无法使用WinSCP .NET程序集从FTP下载文件的代码无法连接,但是我可以在WinSCP GUI中进行连接 - Code to download file from FTP with WinSCP .NET assembly does not connect, but I can connect in WinSCP GUI 无法使用WinSCP .NET程序集将文件下载到目录中 - Unable to download file into directory using WinSCP .NET assembly 使用WinSCP .NET程序集列出具有特定扩展名的文件 - List files with specific extension using WinSCP .NET assembly WinSCP .NET 程序集 - 在 GetFiles 之后删除文件(不是目录) - WinSCP .NET assembly - Remove files (not directories) after GetFiles
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM