Ver Mensaje Individual
  #2  
Antiguo 26-02-2007
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
Lo primero que se me ocurre es buscar los archivos y ordenarlos por la fecha de modificacion. Una vez ordenados, borrar los 10 ultimos resulta sencillo.

Para ordenar los archivos vamos a utilizar el metodo CustomSort del objeto TStringList, que utiliza el algoritmo de ordenamiento Quicksort. La funcion que realiza la busqueda quedaria mas o menos asi:
Código Delphi [-]
function Comparar(List: TStringList; Index1, Index2: Integer): Integer;
begin
  // Para cambiar de orden descendente a ascendente intercambiar Index1 y Index2
  Result:= Integer(List.Objects[Index2]) - Integer(List.Objects[Index1]);
end;

procedure Buscar(Path,Mask: string; Attr: Integer; Lista: TStringList);
var
  SearchRec: TSearchRec;
begin
  if Copy(Path,Length(Path),1) <> '\' then
    Path:= Path + '\';
  if FindFirst(Path + Mask,Attr,SearchRec) = 0 then
  repeat
    Lista.AddObject(Path + SearchRec.Name, TObject(SearchRec.Time));
  until FindNext(SearchRec) <> 0;
  FindClose(SearchRec);
  Lista.CustomSort(Comparar);
end;

Ahora, como ya dije, solo nos falta eliminar los 10 primeros archivos de la lista:
Código Delphi [-]
var
  i: integer;
  Lista: TStringList;
begin
  Lista:= TStringList.Create;
  try
    Buscar('C:\Prueba','*.*',faArchive,Lista);
    i:= 10;
    while (Lista.Count > 0) and (i > 0 ) do
    begin
      DeleteFile(Lista[0]);
      Lista.Delete(0);
      dec(i);
    end;
  finally
    Lista.Free;
  end;
end;
Responder Con Cita