TFindFile question

Please post bug reports, feature requests, or any question regarding the DELPHI AREA products here.

TFindFile question

Postby uwe » August 18th, 2008, 12:55 am

Hi

I would like to use your TFindFile to search a directory for files with a certain extension. Finding those files is easy, but do you know how to add up the number of files that were found which have the same extension? For example:

*.bmp (3 files found)
*.txt (10 files found)
*.wmv (5 files found)

Thx
Uwe
uwe
Member
Member
 
Posts: 2
Joined: August 6th, 2008, 7:58 pm

Postby Kambiz » August 18th, 2008, 2:41 pm

Here is a dirty but simple solution.

Drop a TFindFile and a TMemo on the form, fill the FindFile properties, and use the following code for the event handlers.
I don't describe the code because for sure you can figure it out.

Code: Select all
var
  Extensions: TStringList;

procedure TForm1.FindFile1SearchBegin(Sender: TObject);
begin
  Extensions := TStringList.Create();
  Extensions.CaseSensitive := True;    // for search optimization, we manage case by ourselvs
  Extensions.Duplicates := dupAccept;  // for insert optimization, we don't have duplicates
  Extensions.Sorted := True;           // for search optimization, using binary search
end;

procedure TForm1.FindFile1FileMatch(Sender: TObject; const Folder: String;
  const FileInfo: TSearchRec);
var
  Ext: String;
  Idx: Integer;
begin
  Ext := LowerCase(ExtractFileExt(FileInfo.Name));
  with Extensions do
  begin
    Idx := IndexOf(Ext);
    if Idx < 0 then
      AddObject(Ext, TObject(1))                          // adds first occurrence
    else
      Objects[Idx] := TObject(Integer(Objects[Idx]) + 1); // increment occurrences
  end;
end;

procedure TForm1.FindFile1SearchFinish(Sender: TObject);
var
  I: Integer;
begin
  // here we are going to show the search result inside a memo
  for I := 0 to Extensions.Count - 1 do
    Memo1.Lines.Add(Format('%s = %d', [Extensions[I], Integer(Extensions.Objects[I])]));
  Extensions.Free; // we do not need the extension list anymore
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FindFile1.Execute
end;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Postby uwe » August 18th, 2008, 3:15 pm

Thank you, Kambiz.

Uwe
uwe
Member
Member
 
Posts: 2
Joined: August 6th, 2008, 7:58 pm


Return to DELPHI AREA Products

Who is online

Users browsing this forum: No registered users and 1 guest

cron