版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haimianjie2012/article/details/81290456
1.读取指定文件夹下文件名,保存到vector中
//读取指定文件夹下的文件,将文件路径保存到vector中
void CParseTxtAndExportCSVDlg::getAllFiles(string path, vector<string>& files)
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo; //很少用的文件信息读取结构
string p; //string类很有意思的一个赋值函数:assign(),有很多重载版本
string a = p.assign(path).append("\\*").c_str();
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) {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
}
else
{
string name = fileinfo.name;
int n = name.find("allinfo");
if (n>0)
{
int m = name.find("2017-10-21");
int k = name.find("2017-10-22");
if (m>0||k>0)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
}
}
} while (_findnext(hFile, &fileinfo) == 0); //寻找下一个,成功返回0,否则-1
_findclose(hFile);
}
}
2.将文件名打印到txt文档中
void CParseTxtAndExportCSVDlg::getFileNames()
{
char* filePath = "C:\\Users\\CTOS\\AppData\\Local\\Asia Vision\\VECON-QC\\Logs\\QCMain";
char* distAll = "AllFiles.txt";
getAllFiles(filePath, files);
//将vector中的string保存在txt文件中
ofstream ofn(distAll);
int size = files.size();
ofn << size << endl;
for (int i = 0; i < size; i++) {
ofn << files[i] << endl;
}
ofn.close();
}