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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

ABC-Alpha-Sun-May-12-abc123.xml.gz
ABC-Beta-Sun-May-12-abc123.xml.gz

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

With WinSCP since version 5.8 beta, it's easy. You can use -latest switch of the get command in WinSCP scripting.

In latest versions, it's more complicated. 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.

Also, in case your preferred scripting language is PowerShell, you can also consider using WinSCP .NET assembly with its method Session.ListDirectory:
http://winscp.net/eng/docs/library_session_listdirectory

There are an example on WinSCP site, for all these approaches:
http://winscp.net/eng/docs/script_download_most_recent_file

(I'm the author of WinSCP)

share|improve this answer

Your Answer

 
discard

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.