Listing sub directories
I have some code to find all the sub directories of a directory, but have found it can be VERY slow if there are alot of FILES in the sub directories.
Thats the code im using. It obviously iterates through all the files, not just directories which is causing the slowdown. Is there any faster way of listing sub directories?
void getSubDirs(char *pDir)
sprintf(searchdir, "%s*", pDir);
HANDLE hFind = FindFirstFile(searchdir, &filedata);
if(filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
if(filedata.cFileName != '.')
sprintf(directory, "%s%s%s", pDir, filedata.cFileName, fSep);
Example of output for getSubDirs("world\\");
On first glance, I'm guessing because it re-checks the parent directory over and over again. You put in a check to stop it from checking the current directory again ("."), but not the parent directory ("..").
Edit: d'oh, nm, I see it now.
> but have found it can be VERY slow if there are alot of FILES in the sub directories.
There are two things, neither of which are easy to overcome.
1. Your processor works at <1ns per instruction, and your disk has >1mS seek times (probably closer to 10mS). That's at least a 1E6 difference in performance. Yes, it's going to seem slow every time your code causes the HD to seek somewhere else.
2. As far as the file system is concerned, it only has directory entries. Each one has to be read in turn, then you test the "dwFileAttributes" to see what you've really got.
> char searchdir;
Use PATH_MAX (or is it MAX_PATH) to declare the space for pathnames. 200+ chars isn't enough.
The code checks the first letter of the folder for a '.'. ".." has a '.' in the first letter so it will be excluded.
Hmm, well i tried breaking if it isnt a directory (because i figured directories would come first, so if it isnt a directory there must be no more directories in the folder) but that seemed to skip some :/
There is no guarentee in what order you will receive the files/dirs in a directory, so don't assume any.