需要#include <io.h>
使用了_findfirst, _findnext, _findclose
注意:在X64的系统下_findfirst()返回的handle是long long型的,如果是long 或int 会在_findnext()的时候报错
#include <io.h>
void find_fold(const char* mainDir, std::vector<string> &files)
{
files.clear();
intptr_t hFile; //win10 need long long or intptr_t, long will show error
_finddata_t fileinfo;
char findDir[250];
strcpy_s(findDir, mainDir);
strcat_s(findDir, "\\*.*");
if ((hFile = _findfirst(findDir, &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))//find fold
{
if (fileinfo.name[0] == '.') //avoid . ..
continue;
char filename[_MAX_PATH];
strcpy_s(filename, mainDir);
strcat_s(filename, "\\");
strcat_s(filename, fileinfo.name);
string temfilename = filename;
files.push_back(temfilename);
cout << temfilename << endl;
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void find_file(const char* mainDir, std::vector<string> &files)
{
files.clear();
intptr_t hFile; //win10 need long long or intptr_t, long will show error
_finddata_t fileinfo;
char findDir[250];
strcpy_s(findDir, mainDir);
strcat_s(findDir, "\\*.jpg");
if ((hFile = _findfirst(findDir, &fileinfo)) != -1)
{
do
{
if (!(fileinfo.attrib & _A_SUBDIR))//find fold
{
if (fileinfo.name[0] == '.') //avoid . ..
continue;
char filename[_MAX_PATH];
strcpy_s(filename, mainDir);
strcat_s(filename, "\\");
strcat_s(filename, fileinfo.name);
string temfilename = filename;
files.push_back(temfilename);
cout << temfilename << endl;
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void main()
{
std::vector<string> files;
find_fold(trainDir, files); //find folder first
for (auto d : files)
{
std::vector<string> jpgs;
find_file(d.c_str(), jpgs); //find file in each folder
}
}
参考
test for safe only