Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Ordenar Archivos Directorio en Listbox (https://www.clubdelphi.com/foros/showthread.php?t=88333)

shoulder 21-05-2015 18:08:02

Ordenar Archivos Directorio en Listbox
 
Hola no me doy cuenta como ordenar por fecha de creacion en un listbox los archivos de directorio. Copio lo que fui haciendo y encontrando, funciona bien pero no ordena. Gracias.
Código Delphi [-]
if DirectoryExists(carpeta) then
   ListBox1.Items := ListaArchivos(carpeta+'*.pdf');
Código Delphi [-]
 function ListaArchivos(directorioPadre: string) : TStringList;
  var
    sr: TSearchRec;
  begin
    Result := TStringList.Create;
    if FindFirst(directorioPadre + '*', faAnyFile, sr) = 0 then
      repeat
        if (sr.Attr and faDirectory = 0) or (sr.Name <> '.')
          and (sr.Name <> '..') then
            Result.Add( sr.Name);
      until FindNext(sr) <> 0;
    FindClose(sr);
  end;

Casimiro Notevi 21-05-2015 18:10:37

Recuerda poner los tags correctos para el código, gracias ^\||/

ecfisa 21-05-2015 19:36:12

Hola shoulder.

Código Delphi [-]
...
implementation

uses Contnrs, DateUtils;

type
  TClassFile = class
    Name : string;
    Date : TDateTime;
  end;

procedure SortFilesByDate( Path: string; TS: TStrings );

  function Compare( a, b: Pointer ): Integer;
  begin
    Result:= CompareDateTime( TClassFile(a).Date, TClassFile(b).Date );
  end;

var
  Lista: TObjectList;
  cFile: TClassFile;
  SR   : TSearchRec;
  i    : Integer;
begin
  Path := IncludeTrailingPathDelimiter( Path );
  if FindFirst( Path + '*.*', faArchive, SR ) = 0 then
  begin
    Lista:= TObjectList.Create;
    try
      repeat
        cFile      := TClassFile.Create;
        cFile.Name := SR.Name;
        cFile.Date := FileDateToDatetime( SR.Time );
        Lista.Add( cFile );
      until FindNext( SR ) <> 0;
      FindClose( SR );
      Lista.Sort( @Compare );
      for i:= 0 to Lista.Count-1 do
        TS.Add( TClassFile(Lista[i]).Name );
    finally
      Lista.Free;
    end;
  end;
end;

Ejemplo de uso:
Código Delphi [-]
procedure TForm1.btnSortClick(Sender: TObject);
begin
  SortFilesByDate( 'C:\Una_Carpeta', ListBox1.Items );
end;

Saludos :)

shoulder 21-05-2015 21:14:28

Ordenar
 
Gracias, funciono perfecto.


La franja horaria es GMT +2. Ahora son las 23:54:09.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi