2

We have a shared FTP site with a daily file upload which I need to download and run checks over. I'm simply trying to automate the process. The FTP file is something like name_20150901.xml.

So far I have a batch file to get the file but I can't figure out how to get the latest. Here is my code:

@ftp -i -s:"%~f0"&GOTO:EOF
open ftp.site.com
<user>
<pass>
lcd my local direc
binary
get todaysfile_20150901.xml

What changes do I need to read the files and get the newest one? My end goal is to have a macro that calls this and then reads the file that's grabbed from the FTP and runs my checks.

aLearningLady
  • 1,668
  • 4
  • 17
  • 39
tpayne2345
  • 21
  • 1

1 Answers1

3

There's no easy way to select the most recent file with the ftp.exe.

  • If you know that the file has today's timestamp, you can generate the script dynamically with the today's timestamp. You can use the DATE environment variable, though it has its caveats. A more reliable (and complex) way is to use the wmic os get LocalDateTime.

    See How to get current datetime on Windows command line, in a suitable format for using in a filename?

  • If you can determine the latest file alphabetically, you can:

    • run the ftp.exe with the ls command redirected to a file
    • sort the files by alphabet in a descending order
    • read the first line
    • generate a download script for the second ftp.exe run
  • WinSCP can download the files created within the last 24-hours (1 day):

    winscp.com /command ^
        "open ftp://user:pass@ftp.site.com/" ^
        "lcd c:\my\local\direc" ^
        "get *>=1D" ^
        "exit"
    
  • If you really need to download the latest file (by timestamp), you probably need some more advanced scripting.

    For example there's a guide available for downloading the most recent file with WinSCP.

    (I'm the author of WinSCP)

Community
  • 1
  • 1
Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704