#ifndef COMMON_INCLUDE_H
#define COMMON_INCLUDE_H
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
//#include <sstream>
#include <boost/regex.hpp>
using namespace std;
#endif
#ifndef FIND_FILE_NAME_ACCORD_EXPR_H
#define FIND_FILE_NAME_ACCORD_EXPR_H
#include <windows.h>
#include "CommonInclude.h"
class FindFileNameAccordExpr
{
private:
//bool _isFullPath;
string _dir;
string _searchString;
int _count;
vector<string> _vecfindStrs;
public:
FindFileNameAccordExpr(string dir,string expr);
bool find();
int getCount();
vector<string>::const_iterator getFirstIter();
vector<string>::const_iterator getLastIter();
};
#endif
#include "FindFileNameAccordExpr.h"
FindFileNameAccordExpr::FindFileNameAccordExpr(string dir,string exprStr)
{
_dir = dir;
_searchString = exprStr;
_count = 0;
}
bool FindFileNameAccordExpr::find()
{
if ( _dir == "" || _searchString == "")
{
cout<< "input parameter is null!" << endl;
return false;
}
boost::regex expression(_searchString);
boost::smatch what;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
int find = 1;
string filePath = _dir + "/" + "*.*";
//find all files in current dir,and save in the vector fileNames.
hFind = FindFirstFile(filePath.c_str(),&FindFileData);
string curFileName;
vector<string> fileNames;
if (hFind == INVALID_HANDLE_VALUE)
{
cout<< "Invalid File Handle. GetLastError reports:"<<GetLastError()<<endl;
return false;
}
else
{
fileNames.push_back(FindFileData.cFileName);
while(FindNextFile(hFind,&FindFileData)!=0)
{
if((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)==0)
{
cout<<"File Name is: "<<FindFileData.cFileName<<endl;
curFileName = FindFileData.cFileName;
fileNames.push_back(curFileName);
}
}
FindClose(hFind);
}
if(fileNames.empty())
{
cout<<"Warning>>> The Dir is empty."<<endl;
return false;
}
else
{
vector<string>::const_iterator pos;
for(pos=fileNames.begin(); pos<fileNames.end(); ++pos)
{
if (boost::regex_search(*pos,what,expression))
{
cout <<"Current file name is match the expression,file name is: "<< what[0] <<endl;
_vecfindStrs.push_back(what[0]);
}
}
if(_vecfindStrs.empty())
{
return false;
}
else
{
return true;
}
}
return true;
}
int FindFileNameAccordExpr::getCount()
{
return _count;
}
vector<string>::const_iterator FindFileNameAccordExpr::getFirstIter()
{
return _vecfindStrs.begin();
}
vector<string>::const_iterator FindFileNameAccordExpr::getLastIter()
{
return _vecfindStrs.end();
}
定义main函数验证该类的实现是否正确。
#include "FindFileNameAccordExpr.h"
int main()
{
//FindFileNameAccordExpr fileExpr("D:","*.txt");
FindFileNameAccordExpr fileExpr("D:",".*\\.txt");
if(fileExpr.find())
{
cout<<"find the files"<<endl;
}
getchar();
}
注释:
我使用的是VS2008 实现的,默认建立的空工程是支持Unicode的,因此第一次编译不会通过。
错误原因:WinBase.h中定义如下,
#ifdef UNICODE
#define FindFirstFile FindFirstFileW
#else
#define FindFirstFile FindFirstFileA
#endif // !UNICODE
我们这里使用的都是ASCII,因此使用的FindFirstFileA函数。
The MIT license:
Permission is hereby granted,freeof charge,to any person obtaining a copy of this software and associated documentation files (the "Software"),to deal in the Software without restriction,including without limitation the rights to use,copy,modify,merge,publish,distribute,sublicense,and/or sell
copies of the Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Permission is hereby granted,freeof charge,to any person obtaining a copy of this software and associated documentation files (the "Software"),to deal in the Software without restriction,including without limitation the rights to use,copy,modify,merge,publish,distribute,sublicense,and/or sell
copies of the Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS ORIMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.