Mirror

How to sort directory entries by date / time (Views: 100)

 Problem/Question/Abstract:How to sort directory entries by date / timeAnswer:type  PEntryItem = ^TEntryItem;  TEntryItem = record    FName: string;    FDate: TDateTime;    FSize: Integer;  end;function SortList(item1, item2: Pointer): integer;var  i1: PEntryItem absolute Item1;  i2: PEntryItem absolute Item2;begin  if (i1^.fdate > i2^.fdate) then    result := 1 {greater}  else if (i1^.fdate < i2^.fdate) then    result := -1 {smaller}  else    result := 0; {equals}end;procedure DoTheJob;var  List: TList;  i: Integer;  procedure GetDirEntries(Path: string);  var    Dta: TSearchRec;    P: PEntryItem;  begin    if (Path <> '') and (Path[Length(Path)] = '\') then      setlength(Path, Pred(Length(Path)));    if FindFirst(Path + '\*.*', faAnyFile, Dta) <> 0 then      exit; {Nothing}    repeat      {remove next line if you want these two as well}      if (Dta.Name = '.') or (dta.name = '..') then        continue;      New(P);      P^.FName := Dta.Name;      P^.FSize := Dta.Size;      P^.FDate := FileDateToDateTime(Dta.Time);      List.Add(P);    until      (FindNext(Dta) <> 0);    FindClose(Dta);  end;begin  List := TList.Create;  List.Clear;  try    GetDirEntries('C:\Windows');    if (list.Count > 0) then      List.Sort(SortList);    {Now you have a list with all the files in the directory                         sorted by its date/time field. To access them do as follows:}    for i := 0 to list.count - 1 do    begin      with PEntryItem(List.Items[i])^ do      begin        {I assume you have a Form called MainFrm with a Listbox1}        MainFrm.ListBox1.Items.Add(Format('%s  %d  %s', [FName, FSize,          FormatDateTime('dd/mm/yyyy HH:NN:SS', FDate)]));      end;    end;  finally    {make sure to relase the memory allocated}    while (list.count > 0) do    begin      Dispose(PEntryItem(List.Items));      List.Delete(0);    end;    List.Free;  end;end;

<< Back to main page
{"ft_lang_label":"__label__en","ft_lang_prob":0.5626399,"math_prob":0.82572585,"size":1747,"snap":"2021-43-2021-49","text_gpt3_token_len":555,"char_repetition_ratio":0.110154904,"word_repetition_ratio":0.040816326,"special_character_ratio":0.30623928,"punctuation_ratio":0.27040815,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9735627,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-16T14:34:34Z\",\"WARC-Record-ID\":\"<urn:uuid:67844412-66ac-41f5-b2e9-3750eb27714e>\",\"Content-Length\":\"6562\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:be5e3f10-0810-4a2b-88b2-2afd1e0cc2ed>\",\"WARC-Concurrent-To\":\"<urn:uuid:03ad69c7-8ffe-4185-a5b0-e5183354c347>\",\"WARC-IP-Address\":\"88.151.96.4\",\"WARC-Target-URI\":\"http://users.atw.hu/delphicikk/listaz.php?id=1317&oldal=38\",\"WARC-Payload-Digest\":\"sha1:QQXRKCAIX5VHBAXSLMSVWDVJMWVEWTI7\",\"WARC-Block-Digest\":\"sha1:2HHDZ2FI2HCOIJPWL4NKGOLA3C7VLYFN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323584886.5_warc_CC-MAIN-20211016135542-20211016165542-00328.warc.gz\"}"}