1.读取指定文件夹下包含某个字段的文件名,保存到vector中
void MyCountFile::getAllFiles(string path, vector<string>& mfiles,string findstr)
{
//文件句柄
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), mfiles,findstr);
}
}
else
{
int m = path.find(findstr);
string name = fileinfo.name;
int n = name.find(findstr);
string sub=name.substr(n+1,name.length()-1);
if(m>0||n>0)
{
mfiles.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
}
} while (_findnext(hFile, &fileinfo) == 0); //寻找下一个,成功返回0,否则-1
_findclose(hFile);
}
}
2.将文件名打印到txt文档中
void MyCountFile::GetAndPrintFileNames(string sourcePath,vector<string> &vsrcPath,string findstr)
{
vector<string> sourcefiles;
vector<string> files;
char* sourcedist = "fileList.txt";
getAllFiles(sourcePath, sourcefiles,findstr);
vsrcPath.clear();
vsrcPath=sourcefiles;
//将vector中的string保存在txt文件中
ofstream ofn(sourcedist);
int size = sourcefiles.size();
ofn << size << endl;
for (int i = 0; i < size; i++) {
ofn << sourcefiles[i] << endl;
}
ofn.close();
}
源代码
**.h
#pragma once
#include <string.h>
#include <vector>
#include <list>
using namespace std;
class MyCountFile
{
public:
MyCountFile(void);
~MyCountFile(void);
private:
vector<string> sourcefiles;
public:
void GetAndPrintFileNames(string sourcePath,vector<string> &vsrcPath,string findstr);
void getAllFiles(string path, vector<string>& files,string findstr);
};
**.cpp
#include "StdAfx.h"
#include "MyCountFile.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include "io.h"
MyCountFile::MyCountFile(void)
{
}
MyCountFile::~MyCountFile(void)
{
}
void MyCountFile::GetAndPrintFileNames(string sourcePath,vector<string> &vsrcPath,string findstr)
{
vector<string> sourcefiles;
vector<string> files;
char* sourcedist = "fileList.txt";
getAllFiles(sourcePath, sourcefiles,findstr);
vsrcPath.clear();
vsrcPath=sourcefiles;
//将vector中的string保存在txt文件中
ofstream ofn(sourcedist);
int size = sourcefiles.size();
ofn << size << endl;
for (int i = 0; i < size; i++) {
ofn << sourcefiles[i] << endl;
}
ofn.close();
}
void MyCountFile::getAllFiles(string path, vector<string>& mfiles,string findstr)
{
//文件句柄
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), mfiles,findstr);
}
}
else
{
int m = path.find(findstr);
string name = fileinfo.name;
int n = name.find(findstr);
string sub=name.substr(n+1,name.length()-1);
if(m>0||n>0)
{
mfiles.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
}
} while (_findnext(hFile, &fileinfo) == 0); //寻找下一个,成功返回0,否则-1
_findclose(hFile);
}
}