Windows-style pattern matching

M

Mike C#

Hi all, quick question. Is there a Windows function I can call to compare a
single string like "HELLO.TXT" to a Windows filename/path pattern (possibly
with wildcards) like "H?LL?.*" ? Basically I want to pattern match using
Windows' rules but on a string I pass in as opposed to going through
FindFirstFile, et al.

If not, does anyone know where I can find such a function already
implemented online?

Finally if not that, does anyone know where I can find a list of the
specific rules that Windows uses to pattern match in the FindFirstFile
function?

Thanks
 
C

Carl Daniel [VC++ MVP]

Mike said:
Hi all, quick question. Is there a Windows function I can call to
compare a single string like "HELLO.TXT" to a Windows filename/path
pattern (possibly with wildcards) like "H?LL?.*" ? Basically I want
to pattern match using Windows' rules but on a string I pass in as
opposed to going through FindFirstFile, et al.

No, there is no such function exposed in the public API.
If not, does anyone know where I can find such a function already
implemented online?

I haven't seen one. The rules are complex and subtle (beyond your worst
nightmares).
Finally if not that, does anyone know where I can find a list of the
specific rules that Windows uses to pattern match in the FindFirstFile
function?

I posed the exact rules to microsoft.public.win32.programmer.kernel a couple
years back...

http://groups.google.com/group/micr.../e13654b08ffb5f01?hl=en&lr=&ie=UTF-8&c2coff=1

-cd
 
M

Mike C#

Carl Daniel said:
Mike C# wrote:

No, there is no such function exposed in the public API.

I was afraid of that. Oh well, I found a couple of really basic functions
online that perform similarly (though not exactly), and should be good
enough for my needs. I'm not really doing anything extravagant, just some
really basic wildcard-style filename matches like "H?LLO.*" and "A*.*" type
stuff.

Thanks.
 
B

Ben Voigt

Mike C# said:
I was afraid of that. Oh well, I found a couple of really basic functions
online that perform similarly (though not exactly), and should be good
enough for my needs. I'm not really doing anything extravagant, just some
really basic wildcard-style filename matches like "H?LLO.*" and "A*.*"
type stuff.

Thanks.

That's commonly called "globbing", if you need another term to feed Google.

Also, the filename pattern matching is pretty dumb insofar as you can't have
'*' midstring. So it wouldn't be hard to write a test function.
Split both filename and pattern at the (last) '.'. Walk through the base
(file title), declaring success upon hitting a '*' in the pattern, and
failure if the characters don't match and the pattern isn't '?'. When you
reach the end-of-string start over with the extension.
 
D

Doug Harrison [MVP]

Also, the filename pattern matching is pretty dumb insofar as you can't have
'*' midstring.

I think that was true for DOS, but Windows (>= Win32) handles it OK.
 
C

Carl Daniel [VC++ MVP]

Doug Harrison said:
I think that was true for DOS, but Windows (>= Win32) handles it OK.

Absolutely correct. File specifications with * and ? mid-string have been
fully supported since NT 3.1 more than 10 years ago.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top