1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
uses System.DateUtils;
{$R *.dfm}
type
TDateTimeObject = class(TObject)
private
FFileName: string;
FDateTime: TDateTime;
public
constructor Create(ASearchRec: TSearchRec);
property FileName: string read FFileName;
property DateTime: TDateTime read FDateTime;
end;
function DateTimeObjectStringListCompare(List: TStringList; Index1, Index2: Integer): Integer;
begin
// plus récent au plus ancien
Result := Trunc(MSecsPerDay * (TDateTimeObject(List.Objects[Index2]).DateTime - TDateTimeObject(List.Objects[Index1]).DateTime));
end;
procedure TForm1.Button1Click(Sender: TObject);
Var Chemin : String;
Info : TSearchRec;
I: integer;
Files: TStringList;
begin
Files := TStringList.Create();
try
Chemin := IncludeTrailingPathDelimiter('C:\DEV\Misc\Forum\Files\Find');
If FindFirst(Chemin+'*.*',faAnyFile,Info)=0 Then
try
repeat
If ((Info.Attr And faDirectory)=0) Then
Files.AddObject(Info.Name, TDateTimeObject.Create(Info));
until FindNext(Info)<>0;
finally
FindClose(Info);
end;
Files.CustomSort(DateTimeObjectStringListCompare);
ListBox1.Items.BeginUpdate;
for I := 0 to ListBox1.Items.Count - 1 do
ListBox1.Items.Objects[I].Free();
ListBox1.Items.Clear;
ListBox1.Items.AddStrings(Files);
ListBox1.Items.EndUpdate;
finally
Files.Free();
end;
end;
{ TDateTimeObject }
constructor TDateTimeObject.Create(ASearchRec: TSearchRec);
var
LocalFileTime: TFileTime;
SystemTime: TSystemTime;
begin
FFileName := ASearchRec.Name;
FileTimeToLocalFileTime(ASearchRec.FindData.ftLastWriteTime, LocalFileTime);
FileTimeToSystemTime(LocalFileTime, SystemTime);
FDateTime := SystemTimeToDateTime(SystemTime);
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
I: integer;
begin
for I := 0 to ListBox1.Items.Count - 1 do
ListBox1.Items.Objects[I].Free();
ListBox1.Items.Clear;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
if ListBox1.ItemIndex >= 0 then
with TDateTimeObject(ListBox1.Items.Objects[ListBox1.ItemIndex]) do
Label1.Caption := Format('%s - %s', [FileName, DateTimeToStr(DateTime)])
else
Label1.Caption := '';
end;
end. |
Partager