views:

16

answers:

1

Hy, How to loop through files matching wildcard and find the file that was last date created. This is in VB ... So I have some files that are with a specific prefix and I like to find the one that is last datetime created! How can his be done?

Thank you! Adrian

+1  A: 

You could use the GetFiles method which takes a wildcard mapping pattern and returns the files. If you are using .NET 4.0 you could also use the EnumerateFiles method which returns an IEnumerable<string> instead of an array. Once you get the files you could apply your filtering logic using LINQ extension methods to order the collection by file creation time and get the first element.

Dim result = Directory.
    EnumerateFiles("c:\test", "*.txt", SearchOption.TopDirectoryOnly).
    OrderByDescending(Function(file) New FileInfo(file).CreationTime).
    FirstOrDefault()
Darin Dimitrov