开发者

What is the fastest way to find a file in a directory tree

I have a directory tree similar to the one below. It contains around开发者_StackOverflow中文版 30,000 files in total.

rootDir
rootDir\subDir1
rootDir\subDir1\subSubDir1
rootDir\subDir1\subSubDir2
rootDir\subDir2
rootDir\subDir2\subSubDir1
rootDir\subDir3
...
rootDir\subDirN

What is the fastest way to find a file from a directory structure such as the one above based on it's name using C++ on Windows?


If you do have Windows Desktop Search or Windows Search operating (or the target computer might have it, anyway), you can use ISearchFolderItemFactory (or ISearchDesktop, for WDS) to have it do a search for you.

If there's no pre-existing index, nearly the only way to do this is with FirstFirstFile, FindnextFile and FindClose. I generally recommend against the obvious recursive method of doing the search though -- a breadth-first search is usually at least as fast, and depending on the situation, can easily be twice as fast.

To do a breadth-first search, you maintain a collection (I usually use a priority queue, but a normal queue, stack, etc., will also work) of sub-directories you haven't searched yet. You start a search by entering the starting directory into the collection, and then having your search function do it's thing. Your search runs in a loop, continuing searching until the collection is empty. When it encounters a directory, it adds it to the collection.


Perhaps not the answer you're looking for, but having an index of available files in the file system optimized for the search patterns you support would be fastest.

If you're using some API to do it, it simply depends on how well you write code, profile and improve it. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜