调用 FindFirstFile 和 FindNextFile 可搜索某个目录下的相应文件。
BOOL SearchFilesByWildcard(WCHAR *wildcardPath)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA pNextInfo;
hFile = FindFirstFile(wildcardPath,&pNextInfo);
if(INVALID_HANDLE_VALUE == hFile)
{
return FALSE;
}
WCHAR infPath[MAX_PATH] = {0};
if(pNextInfo.cFileName[0] != '.')
{
printf("Find result = %ws
",pNextInfo.cFileName);
}
while(FindNextFile(hFile,&pNextInfo))
{
if(pNextInfo.cFileName[0] == '.')
{
continue;
}
printf("Find result = %ws
",pNextInfo.cFileName);
}
return FALSE;
}
int main(int argc,char* argv[])
{
setlocale(LC_ALL,"chs");
//查找 abc开头的txt文件
printf("Search 1:
");
SearchFilesByWildcard(L"E:\Test\abc*.txt");
printf("
");
//查找 abc开头的文件
printf("Search 2:
");
SearchFilesByWildcard(L"E:\Test\abc*");
printf("
");
//查找jnt文件
printf("Search 3:
");
SearchFilesByWildcard(L"E:\Test\*.jnt");
printf("
");
//查找文件名为4个字符的txt文件
printf("Search 4:
");
SearchFilesByWildcard(L"E:\Test\????.txt");
printf("
");
//查找包含"档案"的文件
printf("Search 5:
");
SearchFilesByWildcard(L"E:\Test\*档案*");
printf("
");
getchar();
return 0;
}