Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindFirstFile Multiple file types

Tags:

windows

winapi

Is it possible to use Windows API function FindFirstFile to search for multiple file types, e.g *.txt and *.doc at the same time?

I tried to separate patterns with '\0' but it does not work - it searches only the first pattern (I guess, that's because it thinks that '\0' is the end of string).

Of course, I can call FindFirstFile with *.* pattern and then check my patterns or call it for every pattern, but I don't like this idea - I will use it only if there no other solutions.

like image 560
Ivan Avatar asked Jan 08 '12 20:01

Ivan


2 Answers

This is not supported. Run it twice with different wildcards. Or use *.* and filter the result. This is definitely the better choice, wildcards are ambiguous anyway due to support for legacy MS-DOS 8.3 filenames. A wildcard like *.doc will find both .doc and .docx files for example. A filename like longfilename.docx also creates an entry named LONGFI~1.DOC

like image 174
Hans Passant Avatar answered Oct 08 '22 00:10

Hans Passant


The MSDN docs mention nothing about FindFirstFile allowing multiple search patterns, hence it doesn't exist.

In this case your best bet is to scan using an open selection (like C:\\some directory\* or *) and then filter based on WIN32_FIND_DATA's cFileName member, using strrchr (or the appropriate Unicode variant) to find the extension. It should run pretty fast for the small set of characters that make up a file extension.

If you know the that all the extensions are say 3 characters, you should be able to mask it off as *.??? to speed things up.

like image 30
Necrolis Avatar answered Oct 08 '22 00:10

Necrolis