Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to sort directory entries by date / time Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
18-Oct-02
Category
Files Operation
Language
Delphi 2.x
Views
150
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How to sort directory entries by date / time

Answer:

1   type
2     PEntryItem = ^TEntryItem;
3     TEntryItem = record
4       FName: string;
5       FDate: TDateTime;
6       FSize: Integer;
7     end;
8   
9   function SortList(item1, item2: Pointer): integer;
10  var
11    i1: PEntryItem absolute Item1;
12    i2: PEntryItem absolute Item2;
13  begin
14    if (i1^.fdate > i2^.fdate) then
15      result := 1 {greater}
16    else if (i1^.fdate < i2^.fdate) then
17      result := -1 {smaller}
18    else
19      result := 0; {equals}
20  end;
21  
22  procedure DoTheJob;
23  var
24    List: TList;
25    i: Integer;
26  
27    procedure GetDirEntries(Path: string);
28    var
29      Dta: TSearchRec;
30      P: PEntryItem;
31    begin
32      if (Path <> '') and (Path[Length(Path)] = '\') then
33        setlength(Path, Pred(Length(Path)));
34      if FindFirst(Path + '\*.*', faAnyFile, Dta) <> 0 then
35        exit; {Nothing}
36      repeat
37        {remove next line if you want these two as well}
38        if (Dta.Name = '.') or (dta.name = '..') then
39          continue;
40        New(P);
41        P^.FName := Dta.Name;
42        P^.FSize := Dta.Size;
43        P^.FDate := FileDateToDateTime(Dta.Time);
44        List.Add(P);
45      until
46        (FindNext(Dta) <> 0);
47      FindClose(Dta);
48    end;
49  
50  begin
51    List := TList.Create;
52    List.Clear;
53    try
54      GetDirEntries('C:\Windows');
55      if (list.Count > 0) then
56        List.Sort(SortList);
57      {Now you have a list with all the files in the directory 
58  			sorted by its date/time field. To access them do as follows:}
59      for i := 0 to list.count - 1 do
60      begin
61        with PEntryItem(List.Items[i])^ do
62        begin
63          {I assume you have a Form called MainFrm with a Listbox1}
64          MainFrm.ListBox1.Items.Add(Format('%s  %d  %s', [FName, FSize,
65            FormatDateTime('dd/mm/yyyy HH:NN:SS', FDate)]));
66        end;
67      end;
68    finally
69      {make sure to relase the memory allocated}
70      while (list.count > 0) do
71      begin
72        Dispose(PEntryItem(List.Items[0]));
73        List.Delete(0);
74      end;
75      List.Free;
76    end;
77  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC