Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All

I have a requirement that is i have to download the latest files from the directory which is present in FTP.
Is it possible to get only the latest file from the list?

waiting for reply,


Regards
Ramesh
Posted

This code has helped me before...it lets you query an FTP directory and get a list of files batch that match a regular expression and are newer than 'Some Date'

The results returned are sorted by date, so you can obviously get the 'newest' file out of the results


Put the 'Source Code' into your project & you can use as follows

Usage
C#
// Your FTP site
string ftpPath = "ftp://some/localtion/to/ftp/directory/";

// Some expression to match against the files...do they have a consistent name? This example would find XML files that had 'some_string' in the file name
Regex matchExpression = new Regex("^some_string.+\.xml$", RegexOptions.IgnoreCase);

// Only files greater than this value
DateTime cutOff = DateTime.Now.AddDays(-10);

List<ftplineresult> results = FTPHelper.GetFilesListSortedByDate(ftpPath, matchExpression, cutOff);
</ftplineresult>


Source Code

C#
namespace FTP.Utilities
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;
    public enum ListStyle
    {
        Unix,
        Windows
    }
    public class FTPLineResult
    {
        public ListStyle Style { get; set; }
        public string Name { get; set; }
        public DateTime DateTime { get; set; }
        public bool IsDirectory { get; set; }
        public long Size { get; set; }
    }
    /// <summary>
    /// from http://blogs.msdn.com/adarshk/archive/2004/09/15/230177.aspx
    /// </summary>
    public class FTPLineParser
    {
        private Regex winStyle = new Regex(@"^(?<month>\d{1,2})-(?<day>\d{1,2})-(?<year>\d{1,2})\s+(?<hour>\d{1,2}):(?<minutes>\d{1,2})(?<ampm>am|pm)\s+(?<dir>[<]dir[>])?\s+(?<size>\d+)?\s+(?<name>.*)$", RegexOptions.IgnoreCase);
        public FTPLineResult Parse(string line)
        {
            Match match = winStyle.Match(line);
            if (match.Success)
            {
                return ParseMatch(match.Groups, ListStyle.Windows);
            }
            throw new Exception("Invalid line format");
        }
        private FTPLineResult ParseMatch(GroupCollection matchGroups, ListStyle style)
        {
            string dirMatch = (style == ListStyle.Unix ? "d" : "<dir>");
            FTPLineResult result = new FTPLineResult();
            result.Style = style;
            result.IsDirectory = matchGroups["dir"].Value.Equals(dirMatch, StringComparison.InvariantCultureIgnoreCase);
            result.Name = matchGroups["name"].Value;
            result.DateTime = new DateTime(2000 + int.Parse(matchGroups["year"].Value), int.Parse(matchGroups["month"].Value), int.Parse(matchGroups["day"].Value), int.Parse(matchGroups["hour"].Value) + (matchGroups["ampm"].Value.Equals("PM") && matchGroups["hour"].Value != "12" ? 12 : 0), int.Parse(matchGroups["minutes"].Value), 0);
            if (!result.IsDirectory)
                result.Size = long.Parse(matchGroups["size"].Value);
            return result;
        }
    }
    /// <summary>
    /// FTP Class to retreieve files that match an expression and file date cutoff
    /// </summary>
    public class FTPHelper
    {
        /// <summary>
        /// Get a list of files from the FTP server, in a specified path, that match a naming regular expression, and who's file date is after a cutoff
        /// </summary>
        /// <param name="ftpPath">The path the to FTP location</param>
        /// <param name="nameRegex">Regurlar expression to match when finding files</param>
        /// <param name="cutoff">A datetime that files on the FP server must be greater than</param>
        /// <returns></returns>
        public static List<FTPLineResult> GetFilesListSortedByDate(string ftpPath, Regex nameRegex, DateTime cutoff)
        {
            List<FTPLineResult> output = new List<FTPLineResult>();
            FtpWebRequest request = FtpWebRequest.Create(ftpPath) as FtpWebRequest;
            ConfigureProxy(request);
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            FtpWebResponse response = request.GetResponse() as FtpWebResponse;
            StreamReader directoryReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
            var parser = new FTPLineParser();
            while (!directoryReader.EndOfStream)
            {
                var result = parser.Parse(directoryReader.ReadLine());
                if (!result.IsDirectory && result.DateTime > cutoff && nameRegex.IsMatch(result.Name))
                {
                    output.Add(result);
                }
            }
            // need to ensure the files are sorted in ascending date order
            output.Sort(
                new Comparison<FTPLineResult>(
                    delegate(FTPLineResult res1, FTPLineResult res2)
                    {
                        return res1.DateTime.CompareTo(res2.DateTime);
                    }
                )
            );
            return output;
        }
        /// <summary>
        /// set up the web proxy
        /// </summary>
        /// <param name="request"></param>
        private static void ConfigureProxy(FtpWebRequest request)
        {
            request.Proxy = WebRequest.GetSystemWebProxy();
            request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
        }
    }
}
 
Share this answer
 
Comments
Sandeep Mewara 1-Mar-11 10:13am    
For answer... My 5!!!
For Full souce code.. 10!!!
:)
Sandeepdshenoy 17-Dec-13 2:18am    
5*, worked perfectly for me :)
James Rimmer 15-Apr-14 12:42pm    
I know this is an old post, but can it be set to have no date cut off?
pradeep kumar 19-Jul-16 17:12pm    
how to get only last modified date of an folder from FTP?!

Immediate help is appreciated.!!
this[^] might help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900