问题
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.
回答1:
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
DATEenvironment variable, though it has its caveats. A more reliable (and complex) way is to use thewmic 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.exewith thelscommand 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.exerun
- run the
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)
来源:https://stackoverflow.com/questions/32353408/batch-macro-code-to-get-latest-file-from-ftp-site