Getting the oldest file in a directory.

M

Mufasa

Is there an easy way to get the oldest file in a directory without having to
go through all the files comparing the file modified time?

TIA - Jeff.
 
S

Stanimir Stoyanov

To my knowledge, there isn't a native API or a method in .NET Framework to
do this so you will have to go through each of the files.
 
F

Family Tree Mike

Mufasa said:
Is there an easy way to get the oldest file in a directory without having to
go through all the files comparing the file modified time?

TIA - Jeff.

Here is a way to do this. Replace "c:\foo.txt" as needed.

class Program
{
static void Main ( string [ ] args )
{
Process p = new Process ();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c dir /B /OD > c:\foo.txt";
p.Start ();
p.WaitForExit ();
using (FileStream fs = new FileStream ( @"c:\foo.txt", FileMode.Open ))
{
TextReader tr = new StreamReader ( fs );
string txt = tr.ReadLine ();
Console.Out.WriteLine ( txt ); // txt has oldest filename
}
Console.In.ReadLine ();
}
}
 
S

Stanimir Stoyanov

FYI this method still walks through all files in the directory, but this is
done by CMD. However, I would recommend against using a physical path to
redirect the CMD output. Here is an improved version which redirects the
standard process output and reads the first line (the oldest file). It also
allows for checking an arbitrary folder as opposed to the current directory.

static void Main(string[] args)
{
string folderToCheck = @"C:\Windows"; // Remember to set this to
the folder in question

Process p = new Process();

p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = string.Format(@"/c dir ""{0}"" /B /OD",
folderToCheck);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;

p.Start();
p.WaitForExit();

string oldestFile = p.StandardOutput.ReadLine();

// Do something with oldestFile
}
--
Stanimir Stoyanov
http://stoyanoff.info

Family Tree Mike said:
Mufasa said:
Is there an easy way to get the oldest file in a directory without having
to
go through all the files comparing the file modified time?

TIA - Jeff.

Here is a way to do this. Replace "c:\foo.txt" as needed.

class Program
{
static void Main ( string [ ] args )
{
Process p = new Process ();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c dir /B /OD > c:\foo.txt";
p.Start ();
p.WaitForExit ();
using (FileStream fs = new FileStream ( @"c:\foo.txt", FileMode.Open ))
{
TextReader tr = new StreamReader ( fs );
string txt = tr.ReadLine ();
Console.Out.WriteLine ( txt ); // txt has oldest filename
}
Console.In.ReadLine ();
}
}
 
A

Alberto Poblacion

Mufasa said:
Is there an easy way to get the oldest file in a directory without having
to go through all the files comparing the file modified time?

Here is a one line implementation using the LINQ Extension methods:

string oldest = (new DirectoryInfo(path)).GetFiles().OrderBy(f =>
f.LastAccessTime).First().Name;

Of course, internally it does need to go through all the files. I don't
think there is any way to avoid this, since the Operating System does not
index the files by date (only by name if you are using NTFS -- not even that
if you are using FAT).


Here's a complete program that demonstrates this technique:


using System;
using System.Linq;
using System.IO;

namespace OldestFile
{
class Program
{
static void Main(string[] args)
{
string path = args[0];
Console.WriteLine(GetOldestFile(path));
}

private static string GetOldestFile(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
return di.GetFiles().OrderBy(f =>
f.LastAccessTime).First().Name;
}
}
}
 
F

Family Tree Mike

Peter Duniho said:
Because, after all, if it's worth doing wrong, it's worth doing wrong in a
more .NET-compliant way. :)

Seriously, yes...sure, you can use cmd.exe to do the sorting. But why?
No one in their right mind would use code that starts a whole new process
and has to parse text output, just to avoid using the built-in .NET
tools. With LINQ, it's now even more concise, but even without it's not
really that bad.

Pete


I totally agree with you. I was only trying to satisfy the original post
which implied a requirement for no loop in the code. It is debatable
whether the dir sort violates the requirement. Rediculous requirements
often require rediculous coding...
 
S

Stanimir Stoyanov

It is true that it is always better to make use of the built-in
functionality. I simply like to optimize and polish code and that particular
code sample, even if it is not the best in this case is more .NET-friendly
and can show how to redirect process output and the original. It could be
useful in many other process-related scenarios.

This is about sharing ideas and experience, right? :)
 
M

Manu

It is true that it is always better to make use of the built-in
functionality. I simply like to optimize and polish code and that particular
code sample, even if it is not the best in this case is more .NET-friendly
and can show how to redirect process output and the original. It could be
useful in many other process-related scenarios.

This is about sharing ideas and experience, right? :)

What would be the worst case running time of an algo which has to
traverse every file in the directory. I guess it would be the No of
files in that directory.
I am wondering what is the upper limit on the number of files that can
be stored in a NTFS Directory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top