1 out of 1 rated this helpful - Rate this topic

PathMatchSpec function

Applies to: desktop apps only

Searches a string using a Microsoft MS-DOS wildcard match type.

Syntax

BOOL PathMatchSpec(
  __in  LPCSTR pszFile,
  __in  LPCSTR pszSpec
);

Parameters

pszFile [in]

Type: LPCSTR

A pointer to a null-terminated string of maximum length MAX_PATH that contains the path to be searched.

pszSpec [in]

Type: LPCSTR

A pointer to a null-terminated string of maximum length MAX_PATH that contains the file type for which to search. For example, to test whether pszFile is a .doc file, pszSpec should be set to "*.doc".

Return value

Type: BOOL

Returns TRUE if the string matches, or FALSE otherwise.

Examples


#include <windows.h>
#include <iostream>
#include "Shlwapi.h"
using namespace std;
#pragma comment(lib,"shlwapi.lib");

void main(void)
{
// String path name 1.
char buffer_1[ ] = "C:\\Test\\File.txt";
char *lpStr1;
lpStr1 = buffer_1;

// String path name 2.
char buffer_2[ ] = "C:\\Test\\File.bmp";
char *lpStr2;
lpStr2 = buffer_2;

// String path name 3.
char buffer_3[ ] = "*.txt";
char *lpStr3;
lpStr3 = buffer_3;

// String path name 4.
char buffer_4[ ] = "C:\\Test\\File";
char *lpStr4;
lpStr4 = buffer_4;

// Variable to get the return. 
// from "PathMatchSpec"
int retval;

// Test path name 1.
retval = PathMatchSpec(lpStr1,lpStr3);
cout << "The contents of String 1: " << lpStr1
     << "\nThe return value from the function is " << retval << " = TRUE" << endl;

// Test path name 2.
retval = PathMatchSpec(lpStr2,"*.bmp");
cout << "The contents of String 2: " << lpStr2
     << "\nThe return value from the function is " << retval << " = TRUE" << endl;

// Test path name 4.
retval = PathMatchSpec(lpStr4,lpStr2);
cout << "The contents of String 4: " << lpStr4
     << "\nThe return value from the function is " << retval << " = FALSE"<< endl;
}

OUTPUT:
==========
The contents of String 1: C:\Test\File.txt
The return value from the function is 1 = TRUE
The contents of String 2: C:\Test\File.bmp
The return value from the function is 1 = TRUE
The contents of String 4: C:\Test\File
The return value from the function is 0 = FALSE

Requirements

Minimum supported client

Windows 2000 Professional, Windows XP

Minimum supported server

Windows 2000 Server

Header

Shlwapi.h

Library

Shlwapi.lib

DLL

Shlwapi.dll (version 4.71 or later)

Unicode and ANSI names

PathMatchSpecW (Unicode) and PathMatchSpecA (ANSI)

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Does not properly handle many cases
Do not expect this function to work like the command interpreter Cmd.exe. This function does not handle many boundary conditions, nor does it properly handle empty extensions.

The simplest example is this command, which correctly finds the Windows directory on all versions of Windows and MS-DOS 6.x:

dir c:\windows.*

However, this API call returns false on Vista:

BOOL b = ::PathMatchSpec("C:\\Windows", "C:\\Windows.*");

Because I could not paste a table into this page, I've documented other inconsistencies in my blog at http://qualapps.blogspot.com/2009/08/pathmatchspec-problems.html .