?
Solved

Get the last modified filename in a sub directory

Posted on 2011-10-20
6
Medium Priority
?
327 Views
Last Modified: 2012-05-12
I have a directory with multiple sub directories under it. Each sub directory has one or more files in it. I want to get the filename of the file that has been last modified. How do I go about it ?

Thanks,
Aditya
0
Comment
Question by:pmac38CDS
  • 3
  • 3
6 Comments
 
LVL 14

Accepted Solution

by:
nishant joshi earned 2000 total points
ID: 36999852

List<string> files=System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory,"*",System.IO.SearchOption.AllDirectories).ToList();
            DateTime lastmodifiedDateTime=System.DateTime.MinValue;
            string lastmodifiedFilePath=" ";
            foreach(string filename in files)
            {
                if (lastmodifiedDateTime < System.IO.File.GetLastWriteTime(filename))
                {
                    lastmodifiedDateTime = System.IO.File.GetLastWriteTime(filename);
                    lastmodifiedFilePath = filename;
                }
            }
            Console.Write(lastmodifiedFilePath);

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36999995
You could also sort the list.  Something like:
string path = @"c:\some\path";
            List<string> files = new List<string>();
            files.AddRange(System.IO.Directory.GetFiles(path, "*", System.IO.SearchOption.AllDirectories));
            files.Sort(delegate(string file1, string file2)
            {
                return System.IO.File.GetLastWriteTime(file2).CompareTo(System.IO.File.GetLastWriteTime(file1));
            });
            System.IO.FileInfo fi = new System.IO.FileInfo(files[0]);
            MessageBox.Show(fi.LastWriteTime.ToString() + Environment.NewLine + fi.FullName);

Open in new window

0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37000224
is there any need of sort?????
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37000288
Just depends on what you're doing...

What if later they want the five most recently modified files?

...or they want to sort on multiple parameters?

Sorting gives more flexibility.
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37000306
ok. great and what about performance for to get 1 file that was last modified?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37000342
Like I said, it depends on what you're doing.  I was just presenting another valid approach.  It's up to the author to decide what is best for his or her situation.  =)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question