|
#include <string>
#include<io.h>
#include<queue>
class GetFileNumTool
{
public:
//获得所有文件个数
static int GetFileNum(const std::string& inPath)
{
int fileNum = 0;
std::vector<std::string> pathVec;
std::queue<std::string> q;
q.push(inPath);
while (!q.empty())
{
std::string item = q.front(); q.pop();
std::string path = item + "\\*";
struct _finddata_t fileinfo;
auto handle = _findfirst(path.c_str(), &fileinfo);
if (handle == -1) continue;
while (!_findnext(handle, &fileinfo))
{
if (fileinfo.attrib & _A_SUBDIR)
{
if (strcmp(fileinfo.name , ".")==0 || strcmp(fileinfo.name , "..") == 0)continue;
q.push(item + "\\" + fileinfo.name);
}
else
{
fileNum++;
pathVec.push_back(item + "\\" + fileinfo.name);
}
}
_findclose(handle);
}
return fileNum;
}
//文件个数是否超过某个数值
static bool IsFolderFileNumTooBigger(const std::string& inPath,int maxNum)
{
int fileNum = 0;
//std::vector<std::string> pathVec;
std::queue<std::string> q;
q.push(inPath);
bool res = false;
while (!q.empty())
{
std::string item = q.front(); q.pop();
std::string path = item + "\\*";
struct _finddata_t fileinfo;
auto handle = _findfirst(path.c_str(), &fileinfo);
if (handle == -1) continue;
while (!_findnext(handle, &fileinfo))
{
if (fileinfo.attrib & _A_SUBDIR)
{
if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
q.push(item + "\\" + fileinfo.name);
}
else
{
if (++fileNum > maxNum)
{
q = std::queue<std::string>();//清空并结束
res = true;
break;
}
//pathVec.push_back(item + "\\" + fileinfo.name);
}
}
_findclose(handle);
}
return res;
}
//wstring 版本
static bool IsFileNumSmallThanMaxNum(const WCHAR* inPath,int maxNum,int& outNum,std::vector<std::wstring>& outVec)
{
bool res = true;
std::queue<std::wstring> q;
outNum = 0;
q.push(inPath);
//std::vector<std::wstring> vec;
outVec.clear();
while (!q.empty())
{
std::wstring item = q.front(); q.pop();
std::wstring path = item + L"\\*";
WIN32_FIND_DATA winFindData;
HANDLE hTemp = FindFirstFile(path.c_str(), &winFindData);
if (INVALID_HANDLE_VALUE == hTemp) continue;
while (FindNextFile(hTemp, &winFindData))
{
std::wstring fileName = winFindData.cFileName;
if (L"." == fileName || L".." == fileName) continue;
//如果是目录
if (winFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
q.push(item + L"\\" + winFindData.cFileName);
}
else
{
//if (++outNum > maxNum)
//{
// q = std::queue<std::wstring>();//清空并结束
// res = false;
// break;
//}
{
outVec.push_back(item + L"\\" + winFindData.cFileName);
outNum++;
}
}
}
FindClose(hTemp);
}
return res;
}
};
|
|