• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

Counting Files in a Subdirectory

What is the most effiecient way to count the number of files in a given subdirectory?
0
blitz051697
Asked:
blitz051697
1 Solution
 
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now