Re: count files in Delphi

Posted by Joey *DClub* on July 23, 2004

In Reply to: Re: count files in Delphi posted by rachael on July 23, 2004

Here is the whole thing in full source:

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure CountAndDeleteFiles(FilePath: string);
var
  SearchRec: TSearchRec;
  BeforeCount, AfterCount: Integer;
begin
  BeforeCount := 0;
  AfterCount := 0;
  if FilePath[Length(FilePath)] <> '\' then
    FilePath := FilePath + '\';
  if FindFirst(FilePath + '*.*', faAnyFile and not faDirectory, SearchRec) = 0 then
  begin
    Inc(BeforeCount);
    DeleteFile(FilePath + SearchRec.Name);
    while FindNext(SearchRec) = 0 do
    begin
      Inc(BeforeCount);
      DeleteFile(FilePath + SearchRec.Name);
    end;
  end;
  if FindFirst(FilePath + '*.*', faAnyFile and not faDirectory, SearchRec) = 0 then
  begin
    Inc(AfterCount);
    DeleteFile(FilePath + SearchRec.Name);
    while FindNext(SearchRec) = 0 do
    begin
      Inc(AfterCount);
      DeleteFile(FilePath + SearchRec.Name);
    end;
  end;
  FindClose(SearchRec);
  Form1.Label1.Caption := IntToStr(BeforeCount) + '/' + IntToStr(AfterCount);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  CountAndDeleteFiles('C:\');
end;
end.

Note that i just put the procedure at the top of the rest and they i can use it the same way delphi puts uses the built in ones. I passes 'C:\' as a string to the procedure and this will delete all files but not folders within that directory. You could change that to whateva you want ;)

Joey ^__^


[ DelphiLand FAQ ]