void GetAllTypeFileX86(std::map<std::string, std::string>& files,
const std::string& path, const std::string& fileType)
{
int32_t hFile = 0;
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
GetAllTypeFileX86(files, p.assign(path).append("\\").append(fileinfo.name), fileType);
}
}
else
{
std::string fileName = fileinfo.name;
int32_t pos = fileName.rfind('_');
if (pos == std::string::npos || fileType != fileName.substr(pos))continue;
files.insert(std::pair<std::string, std::string>(fileinfo.name, path));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
使用lambda表达式一次性遍历多类型文件。
void GetAllTypeFileX86(const std::string& path,std::function<void(std::string, std::string)> fileTypeCheck);
int main
{
std::map<std::string, std::string>Files1;
std::map<std::string, std::string>Files2;
std::function<void(std::string, std::string)> fun = [&Files1, &Files2]
(IN const std::string& name, IN const std::string& path)
{
int32_t pos = name.rfind('.');
if (pos == std::string::npos)return;
std::string type = name.substr(pos);
std::transform(type.begin(), type.end(), type.begin(), toupper);
if (type == "type1")Files1.insert({ name, path });
else if (type == "type2")Files2.insert({ name, path });
};
GetAllFullTypeFileX86(std::string(pathBuffer) + TEMP, fun);
}
void GetAllTypeFileX86(const std::string& path,std::function<void(std::string, std::string)> fileTypeCheck)
{
int32_t hFile = 0;
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
GetAllTypeFileX86(files, p.assign(path).append("\\").append(fileinfo.name), fileTypeCheck);
}
}
else
{
fileTypeCheck(fileinfo.name, path);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}