'Get files with latest date from SFTP server using WinSCP in batch file

Hi I am totally new to WinSCP and Batch. I have figured out a script to transfer a folder from an SFTP site to my computer but would like to only grab the latest files from the server.

This will be an automated process I'm hoping to schedule to run instead of having to manually update.

@echo off

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\Users\xxxxxx\Desktop\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://User:[email protected]/ -hostkey=""ssh-dss 1024 /xx/xxxxx/xxxxx=""" ^
    "cd /" ^
    "lcd C:\Users\xxxxxx\Desktop" ^
    "get folder1" ^
    "get folder2"^
    "get folder3"^
    "exit"

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)

However I want to go into each of the folders and only pull the latest files instead of the entire folder as the history I have is older than what is still stored on the server.

Any help- even pointing me to a good article on how to use WinSCP and Batch would be greatly appreciated.



Solution 1:[1]

Use the -latest switch of the get command:

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\Users\xxxxxx\Desktop\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://User:[email protected]/ -hostkey=""ssh-dss 1024 xxxx=""" ^
    "cd /" ^
    "lcd C:\Users\xxxxxx\Desktop" ^
    "get -latest folder1/*" ^
    "get -latest folder2/*"^
    "get -latest folder3/*"^
    "exit"

There is actually a dedicated article on WinSCP site about Downloading the most recent file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1