Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to download the latest file from FTP server

Tags:

batch-file

ftp

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.

like image 583
Stan Avatar asked Aug 20 '12 00:08

Stan


People also ask

How to download files from FTP server?

To download the file from FTP server, we use get command. Using that command we can download one time at a time. To download any file from FTP server First login to your FTP server, navigate to the directory and use the following command to download. ftp> get file1.txt 4. Upload Multiple Files to FTP

How do I fetch the latest data from another server via FTP?

Step one is to fetch the latest data from another server via FTP. In Windows you can accomplish this using a script to send ftp commands. A simple example of an ftp script that fetches a single file looks like this:

How to download latest file from FTP using ZS secure FTP?

Download Latest File from FTP using the latest file path variable. Follow these steps to accomplish the task: 1. Drag and drop ZS Secure FTP Task the design panel and link it with Script Task. 2. Let’s use that latest file variable and FTP Connection to download the latest file in the local folder.

What is FTP and how to use it?

FTP (File Transfer Protocol) is the most popular protocol to transfer files (download and upload) from one system to another system. It provides the fastest way to transfer files. There is much application available on Linux and windows to FTP services like vsFTPd, proFTPd for Linux, FileZilla Server for windows.


1 Answers

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)

like image 191
Martin Prikryl Avatar answered Oct 14 '22 15:10

Martin Prikryl