Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 05-21-2001, 10:24 AM
Lafoopsie Lafoopsie is offline
Senior Member
 
Join Date: Jan 2001
Posts: 221
Default Getting the number of files in a folder?

Is there a simple way to get the number of files in a folder (and of a specific type) without having to do FindFirst and FindNext for every file?
Reply With Quote
  #2  
Old 05-21-2001, 10:31 AM
Tavares Tavares is offline
Senior Member
 
Join Date: Feb 2001
Posts: 1,318
Default RE: Getting the number of files in a folder?

The only way I know,right now is using FindFirst like this:

{ procedure GetFiles -
Parameters:
ADirectory: string; // A directory to search for files
Files: TStringList; // The list of files to put the result in
SubFolders: Boolean; // If True, it searches subfolders too }

procedure GetFiles(const ADirectory: string; Files: TStringList; SubFolders: Boolean);
// Helper function to remove any slashes or add them if needed
function SlashSep(const Path, S: string): string;
begin
if AnsiLastChar(Path)^ <> '\' then
Result := Path + '\' + S
else
Result := Path + S;
end;
var
SearchRec: TSearchRec;
nStatus,Num: Integer;
begin
Num := 0;
// First find all the files in the current directory
// You could put something else instead of *.*, such as *.jpeg or *.gif
// to find only files of those types.
nStatus := FindFirst(PChar(SlashSep(ADirectory, '*.*')), 0, SearchRec);
while nStatus = 0 do
begin
Files.Add(SlashSep(ADirectory, SearchRec.Name));
nStatus := FindNext(SearchRec);
end;
FindClose(SearchRec);
// Next look for subfolders and search them if required to do so
if SubFolders then
begin
nStatus := FindFirst(PChar(SlashSep(ADirectory, '*.*')), faDirectory,
SearchRec);
while nStatus = 0 do
begin
// If it is a directory, then use recursion
if ((SearchRec.Attr and faDirectory) <> 0) then
begin
if ( (SearchRec.Name <> '.') and (SearchRec.Name <> '..') ) then
Begin
GetFiles(SlashSep(ADirectory, SearchRec.Name), Files, SubFolders);
Num := Num + 1;
end;
end;
nStatus := FindNext(SearchRec)
end;
FindClose(SearchRec);
end;
end;


Sorry if it's not your answer.

Regards,
Eduardo Tavares
www.tavareswebsite.cjb.net
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 12:07 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.