Monday, February 20, 2017

Tsearch

procedure TForm1.Button1Click(Sender: TObject);
var
  Rec: TSearchRec;
  FileList: array of string;
  DateList: array of TDateTime;
  i: Integer;
  Done: Boolean;
  TempName: string;
  TempDate: TDateTime;
begin
  // Get files list
  if FindFirst('c:\*.*', faAnyFile, Rec) = 0 then
  repeat
    Setlength(FileList, Length(FileList) + 1);
    Setlength(DateList, Length(DateList) + 1);
    FileList[High(FileList)]:= Rec.Name;
    DateList[High(DateList)]:= FileDateToDateTime(Rec.Time);
  until FindNext(Rec) <> 0;
  FindClose(Rec);

  // Sort
  // Bubble sort
  repeat
    Done:= True;
    for i:= 0 to High(FileList) - 1 do
      if DateList[i] > DateList[i + 1] then
      begin
        Done:= False;
        TempName:= FileList[i];
        FileList[i]:= FileList[i + 1];
        FileList[i + 1]:= TempName;

        TempDate:= DateList[i];
        DateList[i]:= DateList[i + 1];
        DateList[i + 1]:= TempDate;

      end;

  until Done;

  // Show in list
  ListBox1.Clear;
  for i:= 0 to High(FileList) do
    ListBox1.Items.Add(FileList[i] + '   ' + DateTimeToStr(DateList[i]));
end;


If you want descending sort then invert the comparison operator to <

      if DateList[i] < DateList[i + 1] then




//-------

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;



//----------

http://www.tek-tips.com/viewthread.cfm?qid=1479527

https://www.experts-exchange.com/questions/20533938/Date-sorted-FileList.html