简体   繁体   中英

Batch file to download the latest file from FTP server

I have a batch file that FTPs CSV files from my web server. I need to download only the most current CSV file.

How do I do that?

This is what I have so far:

open 44.44.44.444
username
password

CD /Client/ABCCompany/

get *.csv

quit
close()

Thanks.

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 in its filename, you can generate the script dynamically with 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 do I get current datetime on the 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 ls command redirected to a file
    • sort the file by alphabet in descending order
    • read the first line
    • generate download script for second ftp.exe run.
  • WinSCP can download the latest file, using the -latest switch of the get command :

     winscp.com /command ^ "open ftp://username:password@ftp.example.com/" ^ "cd /remote/path" ^ "get -latest *.csv" ^ "exit" 

    See also the guide to downloading the most recent file with WinSCP .

  • WinSCP can also download the files created within some interval , eg the last 24-hours (1 day):

     winscp.com /command ^ "open ftp://username:password@ftp.example.com/" ^ "cd /remote/path" ^ "get *.csv>=1D" ^ "exit" 

    or files created since some point in time, eg to download files created since midnight, use today keyword :

     winscp.com /command ^ "open ftp://username:password@ftp.example.com/" ^ "cd /remote/path" ^ "get *.csv>=today" ^ "exit" 

    The today keyword is supported by WinSCP 5.15 and newer only. In older versions, use %TIMESTAMP% syntax , specifically %%TIMESTAMP#yyyy-mm-dd%% , instead of today .

  • With WinSCP, you can implement a download of a file with a timestamp in filename more easily than with the DATE or the wmic os get LocalDateTime (as shown above). Again use the %TIMESTAMP% syntax:

     winscp.com /command ^ "open ftp://username:password@ftp.example.com/" ^ "cd /remote/path" ^ "get %%TIMESTAMP#yyyy-mm-dd%%.txt" ^ "exit" 

    (I'm the author of WinSCP)

You're very likely going to have to do the transfer in two parts. The first issuing a DIR command which should give the most recent file as the last. The having parsed out the last filename from the DIR output use that filename for a subsequent GET.

I do something similar in C#.

The easies way to do this would be to split this into two seperate connections and having a text file in the FTP location which will contain the name of the latest file.

open 44.44.44.444
username
password
CD /Client/ABCCompany/
get latestfile.txt
quit
close()

latestfile.txt will contain the name of the newest file that you need to download. The second script will read the text from the latestfile.txt and pull that file only.

for /F "tokens=*" %%A in (latestfile.txt) do [SET FILE = %%A] 

You'll have to add the above line into the batch file that is calling the secondary FTP script.

open 44.44.44.444
username
password
CD /Client/ABCCompany/
get %FILE%
quit
close()

I repeatedly needed to download a backup file from our production environment for installation in our development environment.

In our case the filename of the file which I want to automate the download for, has the date specified in it: backup_2018_08_03_020003_1048387.bak

So we can get the file by using mget *2018_08_03* in a command line ftp session.

Our backup procedure is run every morning at 01.00 AM, so we have a backup each day that we can fetch.

Of course it would have been prettier and nicer to have a script that fetched the latest backup file based on the backup file timestamps, just in case that something went wrong with the latest backup or the backup file naming format changes. The script is just a script to fetch the backup for internal development purposes so its not a big deal if it breaks. I will look into this later and check whether i can make a cleaner solution.

I made a batch script which just asks for todays backup file with the ordinary ftp command prompt scripting.

It is important to get the formatting of todays date right. It must match the formatting of the date in the filename correctly.

If you want to use the script you should replace the variables with your own information. You should also have write access to the directory where you run it from.

This is the script that I made:

@Echo Off
Set _FTPServerName=xxx.xxx.xx.xxx
Set _UserName=Username
Set _Password=Password
Set _LocalFolder=C:\Temp
Set _RemoteFolder="/path/"
Set _Filename=*%date:~-4,4%_%date:~-7,2%_%date:~-10,2%*
Set _ScriptFile=ftptempscript
:: Create script
 >"%_ScriptFile%" Echo open %_FTPServerName%
>>"%_ScriptFile%" Echo %_UserName%
>>"%_ScriptFile%" Echo %_Password%
>>"%_ScriptFile%" Echo lcd %_LocalFolder%
>>"%_ScriptFile%" Echo cd %_RemoteFolder%
>>"%_ScriptFile%" Echo binary
>>"%_ScriptFile%" Echo mget -i %_Filename%
>>"%_ScriptFile%" Echo quit
:: Run script
ftp -s:"%_ScriptFile%"
del "%_ScriptFile%"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM