views:

126

answers:

4
+1  Q: 

Finding folders

Hello. I want to find how much folders are in folder or I should say how much SubFolreds are in folder. So, how I should do that???

P.S. I'm programing with Delphi

+3  A: 

In addition to the FindFirst trick, you'll need to use recursion, if you want to count all folders in the tree (i.e. all levels) and not just the immediate folder.

Here is a short example of the recursive approach, looks to be exactly what you're looking for. Hint: the Find() method is used recursively.

Recursive Search example at DelphiTricks.com

Chris Thornton
Didn't know about the Delphi tricks site. Thanks. +1
lkessler
A: 

You didn't mention which version of Delphi you use. The latest version has the IOUtils unit which includes the TDirectory class. See an example here: link text

Joe Meyer
I use Delphi 2007
gedO
A: 

It depends on your compiler version. If you are using Delphi 2010, the simplest code I can come up with is this:

uses IOUtils, Types;

function GetSubDirCount(const Path: string): Cardinal;
var
  StrArray : TStringDynArray;
begin
  StrArray := TDirectory.GetDirectories(Path,'*',IOUtils.TSearchOption.soAllDirectories);
  Result := Length(StrArray);
end; 
vcldeveloper