6

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.

6
  • How do u know which one is the latest? Based on a file format or file creation date? Commented Aug 20, 2012 at 0:53
  • possible duplicate of How do I write a Windows batch script to copy the newest file from a directory? Commented Aug 20, 2012 at 1:23
  • I'm not familiar with the scripting of FTP commands, this is what I updated and I get an Invalid Command error: *FOR /F %%I IN ('DIR *.csv /B /O:-D') DO COPY %%I c:*
    – Stan
    Commented Aug 20, 2012 at 1:46
  • What do the /B /O do? I haven't seen these mentioned in other Forfile posts.
    – Stan
    Commented Aug 20, 2012 at 1:59
  • @GregHewgill I appreciate your direction to the page with the Batch script. I'm having a problem understanding the Keys and how to use them. Since this is FTP, there is no Copy command, is there?
    – Stan
    Commented Aug 20, 2012 at 2:35

4 Answers 4

6

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 date/time on the Windows command line in a suitable format for usage in a file/folder name?

  • 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:[email protected]/" ^
        "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, e.g. the last 24-hours (1 day):

    winscp.com /command ^
        "open ftp://username:[email protected]/" ^
        "cd /remote/path" ^
        "get *.csv>=1D" ^
        "exit"
    

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

    winscp.com /command ^
        "open ftp://username:[email protected]/" ^
        "cd /remote/path" ^
        "get *.csv>=today" ^
        "exit"
    

    For the today keyword, make sure you have recent version of WinSCP.

  • 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). Use the %TIMESTAMP% syntax:

    winscp.com /command ^
        "open ftp://username:[email protected]/" ^
        "cd /remote/path" ^
        "get %%TIMESTAMP#yyyy-mm-dd%%.txt" ^
        "exit"
    

    (I'm the author of WinSCP)

0

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#.

0

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()
0

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%"

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