lokelo
lokelo

Reputation: 71

What is the best method to find the newest last modified time of a folder of files?

I have a folder with subfolders of files. I would like to get the newest filename by modified time. Is there a more efficient way of finding this short of looping through each folder and file to find it?

Upvotes: 3

Views: 1891

Answers (6)

Perry Neal
Perry Neal

Reputation: 765

Use the FileSystemWatcher object.

Dim folderToWatch As New FileSystemWatcher
folderToWatch.Path = "C:\FoldertoWatch\"
AddHandler folderToWatch.Created, AddressOf folderToWatch_Created
folderToWatch.EnableRaisingEvents = True
folderToWatch.IncludeSubdirectories = True
Console.ReadLine()

Then, just create a handler (here called folderToWatch_Created) and do something like:

Console.WriteLine("File {0} was just created.", e.Name)

Upvotes: 2

rohancragg
rohancragg

Reputation: 5136

void Main()
{
    string startDirectory = @"c:\temp";
    var dir = new DirectoryInfo(startDirectory);

    FileInfo mostRecentlyEditedFile =
        (from file in dir.GetFiles("*.*", SearchOption.AllDirectories)
         orderby file.LastWriteTime descending
         select file).ToList().First();

    Console.Write(@"{0}\{1}", 
        mostRecentlyEditedFile.DirectoryName, 
        mostRecentlyEditedFile.Name);
}

Upvotes: 0

jwmiller5
jwmiller5

Reputation: 2604

If you have PowerShell installed, the command would be

dir -r | select Name, LastWriteTime | sort LastWriteTime -DESC | select -first 1

This way, the script could run as necessary and you could pass the Name (or full path) back into your system for processing.

Upvotes: 0

Perpetualcoder
Perpetualcoder

Reputation: 13571

If you like Linq you could use linq to object to query your filesystem the way you want. A sample is given here, you can play around and get what you want in probably 4 lines of code. Make sure your app has proper access rights to the folders.

Upvotes: 1

Sean Bright
Sean Bright

Reputation: 120644

Using FileSystemWatcher will work assuming you only need to know this information after the app is loaded. Otherwise you still need to loop. Something like this would work (and doesn't use recursion):

Stack<DirectoryInfo> dirs = new Stack<DirectoryInfo>();
FileInfo mostRecent = null;

dirs.Push(new DirectoryInfo("C:\\TEMP"));

while (dirs.Count > 0) {
    DirectoryInfo current = dirs.Pop();

    Array.ForEach(current.GetFiles(), delegate(FileInfo f)
    {
        if (mostRecent == null || mostRecent.LastWriteTime < f.LastWriteTime)
            mostRecent = f;
    });

    Array.ForEach(current.GetDirectories(), delegate(DirectoryInfo d)
    {
        dirs.Push(d);
    });
}

Console.Write("Most recent: {0}", mostRecent.FullName);

Upvotes: 1

youri
youri

Reputation: 464

depends on how you want to do it. do you want you software to start and then look for it or continuously run and keep track of it?

in the last case its good to make use of the FileSystemWatcher class.

Upvotes: 3

Related Questions