I have a directory with around 15-30 thousand files. I need to just pull the oldest one. In other words the one that was created first. Is there a quick way to do this using C#, other than loading them into a collection then sorting?
To retrieve them in sorted order by the date modified, use either of these C# statements. files = di. GetFiles("*. *").
To search for a directory, use -type d. -printf '%T+ %p\n' prints the last modification date & time of file (defined by %T) and file path (defined by %p). The \n adds a new line. Sort | head -n 1 it sorts the files numerically and passes its output to the head command which displays the 1 oldest file.
ls -lt (what Rahul used) lists the current directory in long format in order by modification date/time, with the newest first and the oldest last. ls -ltr is the reverse of that; oldest first and the newest last.
You will have to load the FileInfo objects into a collection & sort, but it's a one-liner:
FileSystemInfo fileInfo = new DirectoryInfo(directoryPath).GetFileSystemInfos()
.OrderBy(fi => fi.CreationTime).First();
Ok, two lines because it's a long statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With