Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to get all paths to subfolders within a folder (with WinAPIs and C++.) So far the only solution that I found is recursively calling FindFirstFile / FindNextFile but it takes a significant amount of time to do this on a folder with a deeper hierarchy.

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

like image 963
ahmd0 Avatar asked Jan 14 '23 23:01

ahmd0


2 Answers

If you really just need subfolders you should be able to use FindFirstFileEx with search options to filter out non-directories.

The docs suggest this is an advisory flag only, but your filesystem may support this optimization - give it a try.

FindExSearchLimitToDirectories

This is an advisory 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.

like image 145
Steve Townsend Avatar answered Jan 30 '23 00:01

Steve Townsend


A faster approach would be to bypass the FindFirstFile...() API and go straight to the file system directly. You can use DeviceIoControl() with the FSCTL_ENUM_USN_DATA control to access the master file table, at least on NTFS formatted volumes. With that information, you can directly access the records for files/folders, which includes their attributes, parent info, etc. Yes, it would be more work, but it should also be faster since you can optimize the code to access just the pieces you need.

like image 31
Remy Lebeau Avatar answered Jan 30 '23 00:01

Remy Lebeau