积极答复者
win32 验证文件匹配的函数

问题
-
求一个函数,我想的原型如下:
bool isMatch(const char* const file_name, const char* const match_str);
实现这样的功能:验证输入的文件名是否与match_str匹配,比如:
isMatch("rsc1001.jpg", "*.jpg"); //返回的结果为true,因为这个文件名匹配"*.jpg"
在win32 API中有查找文件的函数,可是我现在的文件都是从一个文件中读出来的,现在想找出匹配的文件,我没有找到在内存中比较的函数,我想windows中肯定有这样的函数,因为windows的查找功能也是将文件名读到内存中进行过滤的啊,总不能将这些文件再创建到另一个临时目录中或者自己写一个匹配算法吧
也想了用正则表达式来匹配文件名,可是要用到第三方的库,这是项目中不充许的,所以在些请教一下,windows 的有哪个API函数可以实现相应的功能,C函数或者C++标准库中如果有这样的函数也行的,请赐教
emyueguang
答案
-
http://msdn.microsoft.com/en-us/library/bb773727%28VS.85%29.aspx
- 已标记为答案 VisualElevenModerator 2011年4月16日 1:29
-
try this: PathMatchSpec / PathMatchSpecEx
// MSDN例子程序:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
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
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.- 已标记为答案 VisualElevenModerator 2011年4月16日 1:29
全部回复
-
就是匹配后缀名是否相同?CFileFind类的FindFile/FileNextFile()成员函数可以查找指定目录下相匹配的文件,Win32提供了FindFirstFile/FileNextFile()等函数。
MSDN下提供的例子,稍微修改一下,查找F:盘下的.txt文件
void Recurse(LPCTSTR pstr, LPCTSTR pTxt)
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
// if it's a directory, recursively search it
CString str = finder.GetFilePath();if (finder.IsDirectory())
{
Recurse(str, pTxt);
}
else
{
if(0 == str.Right(3).CompareNoCase(pTxt))
TRACE(_T("%s\n"), (LPCTSTR)str);
}
}
finder.Close();
}void PrintDirs(LPCTSTR str, LPCTSTR txt)
{
Recurse(str, txt);
}
// 调用
void CXXXDlg::OnOK()
{
// TODO: Add extra validation here
PrintDirs(_T("F:"), _T("txt"));
}
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development. -
我知道FindFirstFile()可以在"磁盘目录"下面找到相关的匹配的文件,比如FindFirstFile("*.jpg", &fd),然后就可以遍历当前目录下的jpg类型文件,我现在的问题是:我有一个文件fileNameList.txt,这个文件里面存放了若干的文件名,如下:
fileNameList.txt content:
a.txt
b.txt
c.txt
...
a.jpg
b.jpg
c.jpg
...
a.wav
b.wav
c.wav
...
...
我把这个fileNameList.txt里面的所有文件名字符串读到内存的的一个string数组里面,然后我想在这个数组里面找到相匹配文件,比如
vector<string> isMatch(const vector<string>& fileNameVector, const string& matchStr);
调用它:
vector<string> fileNameVector = ... //获取fileNameList.txt里面的文件名
vector<string> matchFile = isMatch(fileNameVector, "*.jpg");
这个时候isMatch返回的是所有jpg类型的文件名
windows系统既然有查找文件的功能,我想,它也应该有相关的API函数吧,FindFirstFile()是找"磁盘目录"下的匹配文件名,而我想找的是"内存"里面匹配的文件名, windows系统也是将"磁盘目录下的文件名"读到内存里面,然后再进行匹配算法的比较的吧,那windows里面有没有这样一个API函数,让我可以直接将文件名和要匹配的串传给它,然后它看一下,告诉这个文件名与我给的串是否匹配,有没有这样的API函数呢
bool isMatch(const string& fileNameStr, const string& matchStr);
此处调用:
isMatch("a.jpg", "*.jpg"); //return true;
isMatch("a.jpg", "*.*"); //return true;
isMatch("a.jpg", "a.*"); //return true;
isMatch("a.jpg", "a.jpg"); //return true;
isMatch("a.jpg", "*.wav"); //return false;
emyueguang -
http://msdn.microsoft.com/en-us/library/bb773727%28VS.85%29.aspx
- 已标记为答案 VisualElevenModerator 2011年4月16日 1:29
-
try this: PathMatchSpec / PathMatchSpecEx
// MSDN例子程序:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
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
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.- 已标记为答案 VisualElevenModerator 2011年4月16日 1:29