Newbie
Newbie

Reputation: 1111

How to get latest files in C#

I have some files with same name but with different dates. Basically we are finding the files with most recent dates

The file patterns are

<FileNames><YYYYMMDD><FileExtension>

e.g. test_20100506.xls indicates

<FileNames> = test_
<YYYYMMDD> = 20100506
<FileExtension> = .xls

Now in the source folder, the files are

standardization_20100503.xls, standardization_20100504.xls, standardization_20100305.xls, replacement_20100505.xls

As can be seen that, standardization_.xls are 3 in numbers but replacement_.xls is only 1.

The output will be a list of file names whose content will be

standardization_20100504.xls and replacement_20100505.xls

Because among all the standardization_.xls is the most recent one and replacement_.xls is also the same.

I have tried with my own logic but somehow failed.

My idea is as under

private static void GetLatestFiles(ref List<FileInfo> validFiles)
        {
            List<FileInfo> validFilesTemp = new List<FileInfo>();
            for (int i = 0; i < validFiles.Count; i++)
            {
                for (int j = i+1; j < validFiles.Count; j++)
                {
                    string getFileTextX = ExtractText(validFiles[i].Name);
                    string getFileTextY = ExtractText(validFiles[j].Name);
                    if (getFileTextX == getFileTextY)
                    {
                        int getFileDatesX = Convert.ToInt32(ExtractNumbers(validFiles[i].Name));
                        int getFileDatesY = Convert.ToInt32(ExtractNumbers(validFiles[j].Name));

                        if (getFileDatesX > getFileDatesY)
                        {
                            validFilesTemp.Add(validFiles[i]);
                        }
                        else
                        {
                            validFilesTemp.Add(validFiles[j]);
                        }
                    }
                }
            }

            validFiles.Clear();
            validFiles = validFilesTemp;
        }

The ExtractNumbers is:

public static string ExtractNumbers(string expr)
        {
            return string.Join(null, System.Text.RegularExpressions.Regex.Split(expr, "[^\\d]"));
        }

and the ExtractText is

public static string ExtractText(string expr)
        {
            return string.Join(null, System.Text.RegularExpressions.Regex.Split(expr, "[\\d]"));
        }

I am using c#3.0 and framework 3.5

Upvotes: 0

Views: 1598

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038900

class Program
{
    static void Main()
    {
        var files =
            from file in Directory.GetFiles(@"c:\somedirectory")
            let name = Path.GetFileNameWithoutExtension(file)
            let tokens = name.Split('_')
            where tokens.Length > 1
            let date = DateTime.ParseExact(tokens[1], "yyyyMMdd", CultureInfo.InvariantCulture)
            orderby date descending
            select file;

        foreach (var item in files)
        {
            Console.WriteLine(item);
        }
    }
}

Upvotes: 0

Archie
Archie

Reputation: 2579

Use this regex to Get the date out of the full file name.

@"_([0-9]*)\."

Then Convert it to DateTime if you want as

DateTime dt;
 dt = new DateTime();
 dt = DateTime.ParseExact(MyString, "yyyyMMdd",null);

Use dt.CompareTo(dt1).Equals(1) to compare if the date is earlier or later.

Upvotes: 1

Related Questions