1

First of all, I have a list of file name in ftp server:

20130612_00
20130612_06
20130612_12

In Main(), i have something like this

Public Main()
{
    //function to download all file from ftp server to local directory
    DownLoadFileFromFTP();

    //function to open the latest file in the local directory
    OpenLatestFileInLocal();
}

I want to have a function to check the latest file based on the file name in local directory. In this case, the latest file will be 20130612_12

My idea is to first remove the special character and get only the digit number and I am having a list<int> like this now:

2013061200
2013061206
2013061212

Hence, if i check the max value inside the list<int>, I will get to know which one is the latest file.

My ultimate objective is to open the latest file by executing OpenTxtFile(string fileName)

Hence, I have a function something like below:

private void OpenLatestFileInLocal()
{
    // Get list of fileName from local directory
    // Ouput fileList_str = (string){20130612_00 , 20130612_06, 20130612_12}
    List<string> fileList_str = Directory.EnumerateFiles(localDir, "*.txt", SearchOption.AllDirectories).Select(Path.GetFileName).ToList();

    foreach (string fileName in fileList_str)
    {
        fileLIst_int.Add(Convert.ToInt32(RemoveSpecialCharacters(fileName)));
    }
    // Ouput: fileList_int = (int){2013061200 , 2013061206, 2013061212}

    int Latest = fileLIst_int.Max(); //Output: Latest = 2013061212

    // Problem is here.. my OpenTextFile function need to pass the fileName in string
    // But I identify the latest file as an (int) as 2013061212
    // I need to know 2013061212 is actually 20130612_12.txt
    // so that, I can pass the fileName into OpenTxtFile(string fileName)

    OpenTxtFile(fileName_Latest);

}

private void OpenTextFile(string fileName)
{

   // this function will only open file based on string fileName in local directory

}
jhyap
  • 3,779
  • 6
  • 27
  • 47

1 Answers1

1

If you are already using Linq, populate an anonymous class to store both the path and the parsed date (you might also take a look at DateTime.ParseExact versus going to an int):

private void OpenLatestFileInLocal()
{
    var latestFile = Directory
        .EnumerateFiles(localDir, "*.txt", SearchOption.AllDirectories)
        .Select(path => new { Path = path, Date = DateTime.ParseExact(
            Path.GetFileNameWithoutExtension(path), "yyyyMMdd_HH", 
            CultureInfo.InvariantCulture) })
        .OrderByDescending(df => df.Date)
        .Select(df => df.Path)
        .FirstOrDefault();

    if (latestFile != null)
    {
        OpenTxtFile(latestFile);
    }
}
Mitch
  • 21,223
  • 6
  • 63
  • 86
  • All the file name in ftp server is created based on ftp server's system time. I check the latest file based on the file name at `localdir` but not the creation time is because, I cannot confirmed, when I download the file from ftp server to `localdir`, the creation time at local directory is same as the file creation time in ftp. A – jhyap Jun 12 '13 at 04:31
  • This mean, it might happen something like I download the `20130612_12.txt` first and the file creation time at `localdir` is 12:51pm. Then I download `20130612_00.txt` and the file creation time is at `localdir` is 12:52pm. If the latest file is based on creation time, then `20130612_00.txt` will be identified as latest file, which is not something I want :) – jhyap Jun 12 '13 at 04:37
  • @jhyap, I understand. The code above (which previously had a typo) takes the filename as the date and sorts it in descending order. The key line is `DateTime.ParseExact(Path.GetFileNameWithoutExtension(path), "yyyyMMdd_HH", CultureInfo.InvariantCulture)` which will take the path, remove the extension and try and parse it as a date with the format listed (`"yyyyMMdd_HH"` - 4 digit year, 2 digit month, 2 digit day, underscore, 2 digit 24 hour time). – Mitch Jun 12 '13 at 04:38
  • Ok, I get what you mean :) – jhyap Jun 12 '13 at 04:41