简体   繁体   中英

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 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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM