![]() |
|
|
|
#1
|
|||
|
|||
|
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?
|
|
#2
|
|||
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|