Search code examples
c#.netftpftpwebrequestftpwebresponse

Get FTP file details based on datetime in C#


Question: I want to get file Details from FTP server based on some specific datetime without using any 3rd party.

Problem : My FTP server contains 1000s of files so getting all files and after that filtering it takes time.

Is there any Quicker way to do this ?

string ftpPath = "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 

Regex matchExpression = new Regex("^test.+\.xml$", RegexOptions.IgnoreCase);

// DateFilter
DateTime cutOff = DateTime.Now.AddDays(-10);

List<ftplineresult> results = FTPHelper.GetFilesListSortedByDate(ftpPath, matchExpression, cutOff);
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;
}

Solution

  • I have got an alternative solution to do my functionality using FluentFTP.

    Explanation:

    I am downloading the files from FTP (Read permission reqd.) with same folder structure.

    So everytime the job/service runs I can check into the physical path same file(Full Path) exists or not If not exists then it can be consider as a new file. And Ii can do some action for the same and download as well.

    Its just an alternative solution.

    Code Changes:

     private static void GetFiles()
     {
        using (FtpClient conn = new FtpClient())
        {
            string ftpPath = "ftp://myftp/";
    
            string downloadFileName = @"C:\temp\FTPTest\";
    
            downloadFileName +=  "\\";
    
            conn.Host = ftpPath;
            //conn.Credentials = new NetworkCredential("ftptest", "ftptest");
            conn.Connect();
    
            //Get all directories
    
            foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
                FtpListOption.Modify | FtpListOption.Recursive))
            {
                // if this is a file
                if (item.Type == FtpFileSystemObjectType.File)
                {
                    string localFilePath = downloadFileName + item.FullName;
    
                    //Only newly created files will be downloaded.
                    if (!File.Exists(localFilePath))
                    {
                        conn.DownloadFile(localFilePath, item.FullName);
                        //Do any action here.
                        Console.WriteLine(item.FullName);
                    }
                }
            }
        }
    }