Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up file searching with this trick in FindFirstFile-FindNextFile?

Tags:

winapi

I would like to know about the FindFirstFile and the following first FindNextFile. Is it true, that FindFirstFile ALWAYS find the '.' (current folder) and the following FindNextFile ALWAYS the '..' (parent folder)? The mask of course is '*'. I want a little speed up file listing, could I write something, like:

h := FindFirstFile('path\*' ...)     // it finds '.', not process
if h = INVALID_HANDLE_VALUE then ... // some error handling, of course
FindNextFile(...)                    // skipping '..', I suppose, if '.' has found,
                                     // '..' will be too, no handle validity check
while FindNextFile(...) do
  // file/folder processing begins here

So I don't need checking '.' and '..' filenames in every cycle. Sorry for the syntax, I think, I was understandable, and for my english, if I made mistakes.

like image 208
Rhoyd Avatar asked Jan 22 '26 17:01

Rhoyd


1 Answers

There is no guarantee at all that the first two entries will be '. ' and '..'. So your proposed code cannot be used.

You could, I suppose, keep a track in a boolean of whether or not you have seen these two entries and if so skip the checks.

However, checking for these values is not your bottleneck. Enumerating directories involves trips to the disk and system calls. That is the bottleneck. Any attempt to optimise checking for these items will obfuscate your code and yield no discernible performance benefit.

like image 197
David Heffernan Avatar answered Jan 27 '26 02:01

David Heffernan