#pragma once #include <direct.h> #include <io.h> #include <string> #include <vector> size_t getFiles(std::string path, std::vector<std::string>& files, const char *filespec = "*.*", const bool& bIncludeSub = true);
size_t getFiles(std::string path, std::vector<std::string>& files, const char *filespec/* = "*.*"*/, const bool& bIncludeSub/* = true*/)
{
if (path[path.length() - 1] != '\\')
{
path.append("\\");
}
//文件句柄
intptr_t hFile = 0;
//文件信息
_finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append(filespec).c_str(), &fileinfo)) != -1)
{
do
{
//如果不是目录,加入列表
if (!(fileinfo.attrib & _A_SUBDIR))
{
files.push_back(p.assign(path).append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
if (!bIncludeSub)
{
return files.size();
}
//上面只是查找了文件,这里查找目录
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 && !(fileinfo.attrib & _A_SYSTEM))
getFiles(p.assign(path).append(fileinfo.name), files, filespec, true);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
return files.size();
}
转载于:https://www.cnblogs.com/autumoonchina/p/7064847.html