Get latest file from a directory using psftp

1

1

There are many files in a directory on a server which are generated by a process. How to get latest file (order by date descending) that is generated using psftp?

user661981

Posted 2016-11-09T15:05:36.240

Reputation: 11

Answers

3

The psftp does not have such feature.

You would have to:

  • Run psftp once with ls command and output it to a file
  • Parse the output using some scripting language, to find the latest file.
  • Generate ad-hoc download script for the selected file for a second psftp run.

Instead, you can use WinSCP scripting and its get -latest command instead.

Example batch file (.bat):

winscp.com /log=download.log /ini=nul /command ^
    "open sftp://username:password@example.com -hostkey=""...""" ^
    "get -latest /remote/path/* C:\local\path\" ^
    "exit"

There's a guide for Converting PuTTY PSFTP script to WinSCP script.

Or even easier, you can have WinSCP GUI generate the script/batch file for you. All you need to do manually is to add the -latest switch.

(I'm the author of WinSCP)

Martin Prikryl

Posted 2016-11-09T15:05:36.240

Reputation: 13 764