Search code examples
filesystemsc++buildersearchwildcard

Windows API way to search subfolders with wildcards and other criteria in C++?


I think I saw once that it was possible to use the functionality of windows' search feature(s) in code. That it was possible to search for files using a sql query (something like "select filename from filestore where directory = 'c:\somedir' and extention in ('.doc','.txt','.me') and datemodified >= '2009-01-01 00:00:00'"

Anyway, even if there isn't a way to do it with a query, is there any functionality in the Windows API OR any simple code for doing exactly this type of thing?

It seems such an obvious thing for there not to be an easy way to do it.

I have a function that finds all files in a dir (with subdirs) but it's basically dumb. It can't search with wildcards and it can't search with other criteria such as date newer than a given date.

If possible I don't want to download any 3rd party shareware libraries to do this. I'm using C++ builder in vista, and if there are any vista-only functionality for this then that's ok.


Solution

  • Actually, you can use FindFirstFileEx() to implement your search criteria. Unfortunately, most of the time you have to implement this yourself.. It isn't too hard..

    Basically you can check the WIN32_FIND_DATA structure for criteria that you want to search for. For example, if you wanted to find only files with a creation date greater than a certain date, you would check if the WIN32_FIND_DATA structure member ftCreationTime or ftLastWriteTime, depending on your needs..