Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   API de Windows (https://www.clubdelphi.com/foros/forumdisplay.php?f=7)
-   -   Buscar archivos .log y eliminarlos (https://www.clubdelphi.com/foros/showthread.php?t=70835)

mefixxto 12-11-2010 21:43:49

Buscar archivos .log y eliminarlos
 
Necesito de algun codigo que haga que mi aplicacion busque en el disco todos los archivos .log y eliminarlos..

gracias

javier_ecf 18-11-2010 18:28:20

Código Delphi [-]
procedure FindFiles(StartDir, FileMask: string; recursively: boolean; var FilesList: TStringList);
  const
    MASK_ALL_FILES = '*.*';
    CHAR_POINT = '.';
  var
    SR: TSearchRec;
    DirList: TStringList;
    IsFound: Boolean;
    i: integer;
    //cont:Integer;
  begin
    if (StartDir[length(StartDir)] <> '\') then begin
      StartDir := StartDir + '\';
    end;

    // Crear la lista de ficheos en el directorio StartDir (no directorios!)
    IsFound := FindFirst(StartDir + FileMask, faAnyFile - faDirectory, SR) = 0;
    // MIentras encuentre
    while IsFound do  begin
      FilesList.Add(StartDir + SR.Name);
      //FilesList.Add(SR.Name);
      IsFound := FindNext(SR) = 0;
      //cont:=cont+1;
    end;
    FindClose(SR);

    // Recursivo?
    if (recursively) then begin
      // Build a list of subdirectories
      DirList := TStringList.Create;
      // proteccion
      try
        IsFound := FindFirst(StartDir + MASK_ALL_FILES, faAnyFile, SR) = 0;
        while IsFound do
        begin
          if ((SR.Attr and faDirectory) <> 0) and
            (SR.Name[1] <> CHAR_POINT) then
            DirList.Add(StartDir + SR.Name);
            IsFound := FindNext(SR) = 0;
        end;
        FindClose(SR);

        // Scan the list of subdirectories
        for i := 0 to DirList.Count - 1 do
          FindFiles(DirList[i], FileMask, recursively, FilesList);
      finally
       // DirList.Free;
      end;
    end;
  end;

{LISTA Y BORRADO}
Código Delphi [-]
procedure BorrarLogs;
var Lista:TStringList;
var i:integer;
begin
  Lista:=TStringList.Create;
  FindFiles('C:','*.log',true,Lista);

  for i:=0 to Lista.Count -1 do begin
      DeleteFile(PChar(Lista.Strings[i]));
  end;
  Lista.Free;
end;

No lo probe pero creo que funciona.

mefixxto 19-11-2010 23:28:57

Muchisimas gracias, la verdad estaba probando otros metodos pero ya quedaba muy complicado:)


La franja horaria es GMT +2. Ahora son las 12:20:07.

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