Search code examples
sftpwinscp

WinSCP SFTP - Upload latest modified CSV file from folder


I have the following script in WinSCP (version 5.9.5 (Build 7441)) running on a windows machine

# Establish the connection that was saved as MySFTPsite using GUI 
open MySFTPsite
synchronize remote -filemask="*.CSV" "K:\extract\" "/sftp_uploads/"

I call this via a batch file.

This works fine, except it sends ALL the .CSV files in the extract folder.

There is another process that writes a file ever hour into the extract folder.

I want to send only the LATEST .CSV file in this extract folder. How can I do this please?

The remote server that I upload to deletes the file once it processes the file.. so synchronize will always send all the files over and over again.

I'm not sure how to format the filemask in the script or use multiple filemasks (if there is such a thing or even possible).

I do not want to use PowerShell.

I want to keep this as simple as possible and use the script that I have.

Any help would be appreciated.


Solution

  • Use -latest switch of put command:

    put -latest "K:\extract\*.csv" "/sftp_uploads/"
    

    While -filemask="*.CSV>2H may do for you, if the files are added infrequently, it's not the right solution in general.

    For discussion about all possibilities, see WinSCP article on Uploading the most recent file.