Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How do I determine which file is the newest from a remote server that I am connected to via SFTP? My goal is to retrieve a specific file each day, via SFTP. The steps I will follow are:

  1. Connect to server via SFTP

  2. Retrieve the name of the file I need <-- need help with this step

  3. Download it

I can figure out how to connect via SFTP from a script but I am not sure about the best way to go about determining which file to download.

For example, there are many files on the server named like so:


I want to retrieve only that day's Alpha file. I assume that I would check the name for "May-12" to know if this is today's file? Or perhaps I should be checking the file creation date or modified date?

How would I actually retrieve the filename? Perhaps I retrieve the names of all of the files and then sort them somehow?

What would you recommend as the best way to achieve my goal?

share|improve this question

1 Answer 1

You can use WinSCP scripting to gather a content of remote directory into XML file, look though the XML list to find the newest file and download it.

There's an example on WinSCP site, doing exactly what you look for:

Also, in case your preferred scripting language is PowerShell, you can also consider using WinSCP .NET assembly with its method Session.ListDirectory:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.