MikeZ87 1 Posted May 23 Hi Group, Here's a weird one. I'm trying to count all of the files on a drive, so I'm doing a FindFirst('C:\*', faAnyFile, yadda yadda yadda) and counting the files [NOT directories,] and it's finding (exactly) 2184843 files. When I do a dir c:\* /s, it's finding (exactly) 1229974 files. (dir is also reporting 990877 directories.) Either way, these numbers aren't adding up. Any thought on what's going on? var MyTotal : Int64; //This gets set to 0 in the button.click code when FileSearch is initially run. procedure TForm1.FileSearch(const dirName:string); var searchResult: TSearchRec; begin if FindFirst('C:\*', faAnyFile, searchResult)=0 then begin try repeat MyTotal := MyTotal + 1; if (searchResult.Attr and faDirectory)=0 then begin //do stuff end else if (searchResult.Name<>'.') and (searchResult.Name<>'..') then begin FileSearch(IncludeTrailingBackSlash(dirName)+searchResult.Name); end; until FindNext(searchResult)<>0 finally FindClose(searchResult); end; end; end; Share this post Link to post
corneliusdavid 214 Posted May 23 dir c:\* /s This is probably filtering out hidden and system files while FindFirst does not. Share this post Link to post
Attila Kovacs 629 Posted May 24 if FindFirst('C:\*', faAnyFile, searchResult)=0 then begin yadda yadda yadda: if FindFirst(dirName, faAnyFile, searchResult)=0 then begin yadda yadda yadda, yadda yadda? Share this post Link to post
Remy Lebeau 1392 Posted May 24 (edited) 1 hour ago, corneliusdavid said: dir c:\* /s This is probably filtering out hidden and system files while FindFirst does not. There are 2 other mistakes in the FindFirst code - It is counting all of the files AND the subdirectories (despite MikeZ87's claim to the contrary) AND the '.' and '..' entries. Every time FileSearch() is called, it is searching the C:\ root again and again. It is not searching the DirName that is passed in. Try this instead: var MyTotal : Int64; //This gets set to 0 in the button.click code when FileSearch is initially run. procedure TForm1.ButtonClick(Sender: TObject); begin MyTotal := 0; FileSearch('C:'); end; procedure TForm1.FileSearch(dirName: string); var searchResult: TSearchRec; begin dirName := IncludeTrailingPathDelimiter(dirName); if FindFirst(dirName + '*', faAnyFile, searchResult) = 0 then begin // <-- NOT THE ROOT! try repeat if (searchResult.Attr and faDirectory) = 0 then begin Inc(MyTotal); // <-- MOVE HERE! //... end else if (searchResult.Name <> '.') and (searchResult.Name <> '..') then begin FileSearch(dirName + searchResult.Name); end; until FindNext(searchResult) <> 0; finally FindClose(searchResult); end; end; end; That being said, I would suggest a slight modification - make FileSearch() be a function that returns the file count, rather than modifying a class member, eg: procedure TForm1.ButtonClick(Sender: TObject); var TotalFiles : Int64; begin TotalFiles := FileSearch('C:'); // use TotalFiles as needed... end; function TForm1.FileSearch(dirName: string): Int64; var searchResult: TSearchRec; begin Result := 0; dirName := IncludeTrailingPathDelimiter(dirName); if FindFirst(dirName + '*', faAnyFile, searchResult) = 0 then begin try repeat if (searchResult.Attr and faDirectory) = 0 then begin Inc(Result); //... end else if (searchResult.Name <> '.') and (searchResult.Name <> '..') then begin Inc(Result, FileSearch(dirName + searchResult.Name)); end; until FindNext(searchResult) <> 0; finally FindClose(searchResult); end; end; end; Edited May 24 by Remy Lebeau Share this post Link to post