Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
unit Tsort;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
ListRec = record
Name: String;
Time: Integer;
end;
PListRec = ^ListRec;
var
Form1: TForm1;
implementation
{$R *.DFM}
function MyCompareFunc(Item1, Item2: Pointer): Integer;
// Compare returns < 0 if Item1 is less and Item2,
// 0 if they are equal
// and > 0 if Item1 is greater than Item2.
var
res: integer;
begin
res := 0;
if PListRec(Item1)^.Time > PListRec(Item2)^.Time then
Res := 1
else
if PListRec(Item1)^.Time = PListRec(Item2)^.Time then
Res := 0
else
if PListRec(Item1)^.Time < PListRec(Item2)^.Time then
Res := -1;
Result := Res;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MySearchRec: TSearchRec;
errorcode: integer;
SRec: PListRec;
MyList: TList;
i: integer;
begin
MyList := TList.Create;
try
// form list here
errorcode := FindFirst('C:\Windows\*.EXE', faAnyFile, MySearchRec);
try
while errorcode = 0 do
begin
New(SRec);
SRec^.Name := MySearchRec.Name;
SRec^.Time := MySearchRec.Time;
MyList.Add(SRec);
errorcode := FindNext(MySearchRec);
end;
finally
FindClose(MySearchRec);
end;
// now sort it
MyList.Sort(MyCompareFunc);
// output the result
for i := 0 to (MyList.Count - 1) do
begin
SRec := MyList.Items[i];
Memo1.Lines.Add(SRec^.Name + ',' + IntToStr(SRec^.Time));
end;
finally
// deallocate the whole list
for i := 0 to (MyList.Count - 1) do
begin
SRec := MyList.Items[i];
Dispose(SRec);
end;
MyList.Destroy;
end;
end;
end.
const
DirectoryPath = 'C:\data\';
procedure TForm1.Button1Click(Sender: TObject);
var
tsr: TSearchRec;
list: TStringList;
x: integer;
begin
list := TStringList.Create;
list.Sorted := true;
try
// Build list of files in time creation order
if FindFirst( DirectoryPath + '*.*', 0, tsr ) = 0 then begin
repeat
list.Append( Format( '%.8x %s', [ tsr.Time, tsr.Name ] ) );
until FindNext( tsr ) <> 0;
FindClose( tsr );
end;
// Process these files one at a time
for x := 0 to list.count - 1 do
Process( DirectoryPath + Copy( list[x], 9, Length(list[x]) ) );
finally
list.free;
end;
end;
procedure TForm1.Process(filename: string);
begin
memo1.Lines.Append( filename );
end;
The code uses TStringList rather than TList because TStringList can be sorted by setting the Sorted property to true.