473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Getting Direcotry files sorted by date, is it possible?

I wondering if it possilble to get the directory files ordered by creation date

string [] files = System.IO.Directory.GetFiles(strPath,"*.msg")

can I sort the returned file by creation date?

or is there a ready method do the jobthat in .NET

Nov 16 '05 #1
2 52692
Raed Sawalha <Ra*********@discussions.microsoft.com> wrote:
I wondering if it possilble to get the directory files ordered by creation date

string [] files = System.IO.Directory.GetFiles(strPath,"*.msg")

can I sort the returned file by creation date?


You need to implement your own IComparer do the specialized sort:

using System;
using System.IO;
using System.Collections;

public class SortFiles
{
public class CompareFileByDate :IComparer
{
int IComparer.Compare(Object a, Object b)
{
FileInfo fia = new FileInfo((string)a);
FileInfo fib = new FileInfo((string)b);

DateTime cta = fia.CreationTime;
DateTime ctb = fib.CreationTime;

return DateTime.Compare(cta, ctb);
}
}

public static void Main()
{
string [] files = System.IO.Directory.GetFiles("..");

IComparer fileComparer = new CompareFileByDate();
Array.Sort(files, fileComparer);

foreach ( string f in files )
{
Console.WriteLine(f);
}

}
}

/J\
Nov 16 '05 #2
Another way is this one (no need of an extra class):

// get your files (names)
string[] fileNames = Directory.GetFiles("c:\\Temp\\", "*.*");

// Now read the creation time for each file
DateTime[] creationTimes = new DateTime[fileNames.Length];
for (int i=0; i < fileNames.Length; i++)
creationTimes[i] = new FileInfo(fileNames[i]).CreationTime;

// sort it
Array.Sort(creationTimes,fileNames);

// and print for test
Console.WriteLine("Files ordered by creation time");
for (int i=0; i < fileNames.Length; i++)
Console.WriteLine("{0}: {1}", creationTimes[i], fileNames[i]);
HTH,
Stefan
Raed Sawalha schrieb:
I wondering if it possilble to get the directory files ordered by creation date

string [] files = System.IO.Directory.GetFiles(strPath,"*.msg")

can I sort the returned file by creation date?

or is there a ready method do the jobthat in .NET

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Lorn Davies | last post by:
Hi there, I'm a Python newbie hoping for some direction in working with text files that range from 100MB to 1G in size. Basically certain rows, sorted by the first (primary) field maybe second...
6
by: yoda | last post by:
This feels like a stupid question but I'll ask it anyway. How can I process files chronologically (newest last) when using os.walk()?
5
by: henrycortezwu | last post by:
Hi All, I'm trying to get the "groups" of a user that belongs to a domain by just passing the user's userID & domain. Is this possible? What I tried, and is working is the ff code ( i can get...
5
by: Carol | last post by:
Hi, I am in a problem. I am sending files to a particular folder one after the another. Than obviosly there create time is different. Now suppose if there are 50 files and i want to retrive the...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
7
by: Hitesh | last post by:
Hi, I have a small script here that goes to inside dir and sorts the file by create date. I can return the create date but I don't know how to find the name of that file... I need file that is...
1
by: helpmeplease213 | last post by:
Hello, I have a form which has a option group, when a option in the group is selected it brings up a text box, you then enter a parameter into that text box and the data should be sorted out...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
3
by: lion cave | last post by:
Hello, I would like to ask on how to sort files by date modified and displaying the top 20 latest date. Heres, my sample function that would return the files to be displayed function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.