djur1
djur1

Reputation: 39

.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. I'm using WinSCP for downloading and uploading files.

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. 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.

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. 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?

Upvotes: 2

Views: 1394

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202444

There's a whole article on WinSCP site on How do I transfer new/modified files only?

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.

  • 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 + "|*/";
    

Upvotes: 0

codeulike
codeulike

Reputation: 23064

It doesn't look like WinSCP can access the Created Date of the files.

Unless you can do something to make the files 'different' when you re-upload them (e.g. put them in a different folder) then you best option might be:

  • Forget about using FileMask
  • Use WinSCP method EnumerateRemoteFiles to get a list of the files
  • Loop through them yourself (its a collection of RemoteFileInfo objects
  • 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

Upvotes: 0

Related Questions