[英].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: 除非在重新上传文件时可以做一些事情来使文件“不同”(例如,将它们放在另一个文件夹中),否则最佳选择可能是:
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.