MCountFile类,统计指定文件夹下包含某个字段文件数目

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);

	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haimianjie2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值