successed
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <io.h>
using namespace std;
void getAllFiles(string path, vector<string>& files, string postfix)
{
// file handle
long hFile = 0;
// file info. struct
struct _finddata_t fileinfo;
string pathp;
if ((hFile = _findfirst(pathp.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR)) // is folder?
{
// is folder
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
//files.push_back(p.assign(path).append("/").append(fileinfo.name));//saving folder name
getAllFiles(pathp.assign(path).append("/").append(fileinfo.name), files, postfix);//find postfix file recursively
}
}
else //not folder
{
string filestr = fileinfo.name;
string prepostfix = "." + postfix;
int idx = filestr.find(prepostfix);
string poststr = filestr.substr(idx, filestr.size());
if (0 == poststr.compare(prepostfix))
files.push_back(pathp.assign(path).append("/").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0); //find next
_findclose(hFile);
}
}
//测试
void main()
{
string basedir = "D:/video/t/";
string savingname = "filename.txt";
string postfix = "hik"; // specify postfix
vector<string> files;
getAllFiles(basedir, files, postfix);
int filenum = files.size();
// saving as txt file
ofstream outfilestream(savingname); // output file stream
outfilestream << filenum << endl;
for (int i = 0; i < filenum; i++)
{
outfilestream << files[i] << endl;
}
outfilestream.close();
}