遍历文件夹

 //返回.exe的文件名

void FindExeFile(std::string path, std::string mode, std::set<std::string> &saveExeFile)
{
_finddata_t file;
intptr_t HANDLE;
std::string pattern{ "(.*).exe" };
std::regex reg(pattern);
std::string Onepath = path + mode;
HANDLE = _findfirst(Onepath.c_str(), &file);
if (HANDLE == -1L)
{
LOG_WARN << "can not match the folder path";
return;
}
do
{
//判断是否有子目录
if (file.attrib & _A_SUBDIR)
{
//判断是否为"."当前目录,".."上一层目录
if ((strcmp(file.name, ".") != 0) && (strcmp(file.name, "..") != 0))
{
std::string newPath = path + "\" + file.name;
FindExeFile(newPath, mode, saveExeFile);
}
}
else
{
bool ret = std::regex_match(file.name, reg);

//找到.exe文件
if (ret)
{
saveExeFile.insert(file.name);
}
}
} while (_findnext(HANDLE, &file) == 0);
_findclose(HANDLE);
}





    //返回.exe所在的绝对路径

void FindExeFileWithPath(std::string path, std::string mode, std::set<std::string> &saveExeFile)

{
_finddata_t file;
intptr_t HANDLE;
std::string pattern{ "(.*).exe" };
std::regex reg(pattern);
std::string Onepath = path + mode;
HANDLE = _findfirst(Onepath.c_str(), &file);
if (HANDLE == -1L)
{
return;
}
do
{
//判断是否有子目录
if (file.attrib & _A_SUBDIR)
{
//判断是否为"."当前目录,".."上一层目录
if ((strcmp(file.name, ".") != 0) && (strcmp(file.name, "..") != 0))
{
std::string newPath = path + "\" + file.name;
FindExeFile(newPath, mode, saveExeFile);
}
}
else
{
bool ret = std::regex_match(file.name, reg);

//找到.exe文件
if (ret)
{
saveExeFile.insert(path + "\" + file.name);
}
}
} while (_findnext(HANDLE, &file) == 0);
_findclose(HANDLE);
}



void main()

{

  std::vector<std::string> exeFile;

      std::string path="C:\Windows";

     std::mode="*\*";

     FindExeFile(path, mode, exeFile);
}
原文地址:https://www.cnblogs.com/gd-luojialin/p/12214701.html