Excluir arquivos

Delphi

15/05/2003

Eu estou precisando saber com faço para excluir alguns arquivo, explicando melhor:
Faço backups automáticos e queria que só permanecessem um determinado nº de arquivos, por Ex.: o que fosse mais velho doque osa 5 últimos fossem apagados.

Desde já agradeço.


Simoes

Simoes

Curtidas 0

Respostas

Simoes

Simoes

15/05/2003

Já que ninguem respondeu, eu mesmo dou a resposta:

function TFPMonitor.CountFiles(Path, Mask: string): integer;
var
count: Integer;

procedure SearchForFiles(const path, mask: string);
var
SR: TSearchRec;
begin
if FindFirst(path + mask, faAnyFile, SR) = 0 then
try
repeat
inc(Count);
until FindNext(SR) <> 0
finally
FindClose(SR);
end;
end;

begin
count := 0;
if (Path <> \´\´) and (Path[length(path)] <> \´\\\´) then
Path := Path + \´\\\´;
SearchForFiles(path, mask);
result := count;
end;


function TFPMonitor.GetFileDate(Arquivo: String): string;
var
FHandle: integer;
begin
FHandle := FileOpen(Arquivo, 0);
try
Result := DateTimeToStr(FileDateToDateTime(FileGetDate(FHandle)));
finally
FileClose(FHandle);
end;
end;

procedure TFPMonitor.ApagaVelho(Dir: string);
var
SR: TSearchRec;
I: integer;
A: string;
begin
I := FindFirst(Dir + \´\\\´ + \´*.gbk\´, faAnyFile, SR);
while I = 0 do
begin
A := Dir + \´\\\´ + SR.Name;
if (CountFiles(Dir,\´*.gbk\´) > 4) or (StrToDateTime(GetFileDate(A)) < (now - FPConfig.Dias)) then
begin
if (SR.Attr and faDirectory) <> faDirectory then
if not DeleteFile(A) then
ShowMessage(\´Não consegui excluir \´+ A);
I := FindNext(SR);
end
else
I := FindNext(SR);
end;
FindClose(SR);
end;


GOSTEI 0
POSTAR