Counting Files in a Subdirectory

What is the most effiecient way to count the number of files in a given subdirectory?
blitz051697Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
Hi Blitz,
some else asked similer a while ago about ditrectory properties,here was answer:

procedure TForm1.Button1Click(Sender: TObject);
var
TotFolder: Integer;
TotFile: Integer;
TotSize: DWORD;

procedure ScanPath(Path: string);
var
F: TSearchRec;
begin
try
if FindFirst(Path + '*.*', faAnyFile, F) = 0 then
repeat
if (F.Name <> '.') and (F.Name <> '..') then
begin
if F.Attr and faDirectory <> 0 then
begin
Inc(TotFolder);
ScanPath(Path + F.Name + '\');
end else Inc(TotFile);

Inc(TotSize, F.Size);
end;
until FindNext(F) <> 0
else
begin
TotFile := 0;
TotSize := 0;
end;
finally
FindClose(F);
end;
end;

begin
TotFolder := 0;
TotFile := 0;
TotSize := 0;
ScanPath('C:\');
ShowMessage(Format('%d folders %d files, %d MB (%d bytes)',
[TotFolder, TotFile, TotSize div (1024 * 1024), TotSize]));
end;

Do not forget to add '\' to the end of the path, example: do not write ScanPath('C:\Windows') instead write ScanPath('C:\Windows\')...

hope it helps ya
Regards Barry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.