Download the latest file from an FTP server

garci

I have to download the latest file from an FTP Server. I know how download the latest file from my computer, but I don't how download from an FTP server.

How can I download the latest file from a FTP Server?

This is my program to download the latest file from my Computer

public Form1()
    {
        InitializeComponent();

        string startFolder = @"C:\Users\user3\Desktop\Documentos XML";

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);

        IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

        IEnumerable<System.IO.FileInfo> fileQuerry =
            from file in fileList
            where file.Extension == ".txt"
            orderby file.CreationTimeUtc
            select file;

        foreach (System.IO.FileInfo fi in fileQuerry)
            {
                var newestFile =
                (from file in fileQuerry
                 orderby file.CreationTimeUtc
                 select new { file.FullName, file.Name })
                 .First();
                textBox2.Text = newestFile.FullName;
            }
    }

OK, with this code I know the date of the last file, but How I know the name of this file????????

Martin Prikryl

You have to retrieve timestamps of remote files to select the latest one.

Unfortunately, there's no really reliable and efficient way to retrieve modification timestamps of all files in a directory using features offered by .NET framework, as it does not support the FTP MLSD command. The MLSD command provides a listing of remote directory in a standardized machine-readable format. The command and the format is standardized by RFC 3659.

Alternatives you can use, that are supported by .NET framework:


Alternatively you can use a 3rd party FTP client implementation that supports the modern MLSD command.

For example WinSCP .NET assembly supports that.

There's even an example for your specific task: Downloading the most recent file.
The example is for PowerShell and SFTP, but translates to C# and FTP easily:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "username",
    Password = "password",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Get list of files in the directory
    string remotePath = "/remote/path/";
    RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);

    // Select the most recent file
    RemoteFileInfo latest =
        directoryInfo.Files
            .OrderByDescending(file => file.LastWriteTime)
            .First();

    // Download the selected file
    string localPath = @"C:\local\path\";
    string sourcePath = RemotePath.EscapeFileMask(remotePath + latest.Name);
    session.GetFiles(sourcePath, localPath).Check();
}

(I'm the author of WinSCP)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python FTP server download Latest File with specific keywords in filename

From Dev

Download/copy file from FTP server in Java

From Dev

Get the latest file from a remote server from an FTP in Unix

From Dev

Get the latest file from a remote server from an FTP in Unix

From Dev

unix ftp script to get latest file from server

From Dev

Batch file - Download the latest FTP folder

From Dev

Batch file - Download the latest FTP folder

From Dev

How to download a file from FTP server to Android device?

From Dev

Download CSV file from FTP server to save locally and process for Uploading

From Dev

Unable to download and save file from FTP server with Ruby

From Dev

How to download specific file from FTP server using WinSCP?

From Dev

How to download only the latest file from SFTP server with Paramiko?

From Dev

How can I download a file from a FTP server, then automatically delete it from the server once the download completes?

From Dev

Get the latest updated file from FTP Folder

From Dev

Get the latest updated file from FTP Folder

From Dev

How I download file on FTP server in browser?

From Dev

How to get latest modified csv file values from ftp server and insert it into mysql table

From Dev

Using FtpWebResponse to download file - file downloads even after removed from ftp server?

From Dev

Using FtpWebResponse to download file - file downloads even after removed from ftp server?

From Dev

How to Download file from one FTP and then Upload file to different FTP?

From Dev

Download file from url and upload into ftp

From Dev

Download file weekly from FTP to HDFS

From Dev

Download a file from ftp and save in the same folder

From Dev

FTP to download file from one linux to another

From Dev

Download a csv file from ftp with ruby sinatra

From Dev

Download a file from the latest stable Jenkins build

From Dev

Simultaneously download from an FTP server different parts of the same file with multiple connection

From Dev

How to download a file name starting with "xyz" from server in Spring integration ftp?

From Dev

How to download a blob file from Azure Storage and save it to an FTP server using Powershell?

Related Related

  1. 1

    Python FTP server download Latest File with specific keywords in filename

  2. 2

    Download/copy file from FTP server in Java

  3. 3

    Get the latest file from a remote server from an FTP in Unix

  4. 4

    Get the latest file from a remote server from an FTP in Unix

  5. 5

    unix ftp script to get latest file from server

  6. 6

    Batch file - Download the latest FTP folder

  7. 7

    Batch file - Download the latest FTP folder

  8. 8

    How to download a file from FTP server to Android device?

  9. 9

    Download CSV file from FTP server to save locally and process for Uploading

  10. 10

    Unable to download and save file from FTP server with Ruby

  11. 11

    How to download specific file from FTP server using WinSCP?

  12. 12

    How to download only the latest file from SFTP server with Paramiko?

  13. 13

    How can I download a file from a FTP server, then automatically delete it from the server once the download completes?

  14. 14

    Get the latest updated file from FTP Folder

  15. 15

    Get the latest updated file from FTP Folder

  16. 16

    How I download file on FTP server in browser?

  17. 17

    How to get latest modified csv file values from ftp server and insert it into mysql table

  18. 18

    Using FtpWebResponse to download file - file downloads even after removed from ftp server?

  19. 19

    Using FtpWebResponse to download file - file downloads even after removed from ftp server?

  20. 20

    How to Download file from one FTP and then Upload file to different FTP?

  21. 21

    Download file from url and upload into ftp

  22. 22

    Download file weekly from FTP to HDFS

  23. 23

    Download a file from ftp and save in the same folder

  24. 24

    FTP to download file from one linux to another

  25. 25

    Download a csv file from ftp with ruby sinatra

  26. 26

    Download a file from the latest stable Jenkins build

  27. 27

    Simultaneously download from an FTP server different parts of the same file with multiple connection

  28. 28

    How to download a file name starting with "xyz" from server in Spring integration ftp?

  29. 29

    How to download a blob file from Azure Storage and save it to an FTP server using Powershell?

HotTag

Archive