Is there a faster alternative to listing folders than FindFirstFile / FindNextFile with C ++?

I need to get all the paths to subfolders inside a folder (using WinAPI and C ++.) So far, the only solution I have found is to call FindFirstFile / FindNextFile recursively , but it takes considerable time to do this in a folder with a deeper hierarchy.

So, I was wondering, just to get the folder names, is there a faster approach?

+4
source share
3 answers

This is the fastest approach you may encounter. You can also use a different thread to manage directory enumerations, since it takes a lot of time. even Microsoft File Explorer will spend some time if there are many subfolders / files in the directory.

Another thing here is that you can list directories once and then register for any updates. therefore, the cost of listing a folder should be done only once during startup.

+3
source

If you really need subfolders, you can use FindFirstFileEx with search options to filter out non-directories.

The docs suggest this is just a warning flag, but your file system may support this optimization - try it.

FindExSearchLimitToDirectories

This is a warning flag. If the file system supports directory filtering, the function searches for a file that matches the specified name and is also a directory. If the file system does not support directory filtering, this flag is silently ignored.

+3
source

A faster approach is to bypass the FindFirstFile...() API and directly to the file system. You can use DeviceIoControl() with the FSCTL_ENUM_USN_DATA element to access the main file table, at least in volumes with NTFS formatting. Using this information, you can directly access records for files / folders, including their attributes, parent information, etc. Yes, it will be more work, but it should also be faster, since you can optimize the code to access only the parts you need.

+3
source

All Articles