Download last two weeks of files from a FTP folder

Advertisement

gilbreen
Joined:
Posts:
4

Download last two weeks of files from a FTP folder

There are two files generated every day by one of our partner agencies, one in the morning and one in the evening and they are placed on their FTP site. The file's naming structure starts with the current date and then adds a randomly generated number.

Their FTP site holds all generated files going back to June 2022. We currently use a script with WinSCP to synchronize their FTP site to a local folder which means our local folder contains all files going back to June 2022.

Our agency only needs the last two weeks of files at any give time. Instead of keeping all the files back to June 2022 in our local folder, is there a way to have WinSCP only download/synchronize the files for the last two weeks?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,041
Location:
Prague, Czechia

Re: Download last two weeks of files from a FTP folder

It's easy if you can rely on file timestamps:
get /remote/path/*>14D
See https://winscp.net/eng/docs/file_mask#size_time

If you have to rely on the date in the filename, it is more difficult. You can use 14 get commands, one for each day (or one command with 14 masks):
cd /remote/path
get %TIMESTAMP#yyyy-mm-dd%*
get %TIMESTAMP-1D#yyyy-mm-dd%*
get %TIMESTAMP-2D#yyyy-mm-dd%*
...
get %TIMESTAMP-13D#yyyy-mm-dd%*
(assumes that filename has format yyyy-mm-dd*)
See https://winscp.net/eng/docs/scripting#timestamp

Reply with quote

gilbreen
Joined:
Posts:
4

Re: Download last two weeks of files from a FTP folder

Thank you for the assistance. I adjusted our script and the first time the script ran, it downloaded the last 14 days of files. However, on subsequent runs of the script, it downloads the newer files, but is not removing the files that are older than 14 days.

The current script contains the following:
option batch on
option confirm off
open ftp://remote_ftp_site
option transfer binary
get /remote/path/*>14D v:\Test\*
exit
Where v: is one of the local drives and I created the Test folder as the destination.

Is there a way to adjust the script so it deletes the files that no longer meet the 14 day requirement?

Reply with quote

martin
Site Admin
martin avatar

Re: Download last two weeks of files from a FTP folder

If you do not need the old files, just delete complete v:\Test before even starting WinSCP script.

Reply with quote

gilbreen

Just following up that everything is working as expected. I created separate task to remove the contents of the folder prior to the automated syncing and it is working great. Thanks for the help!

Reply with quote

Advertisement

You can post new topics in this forum