方便的遍历文件目录的类

 次需要遍历文件进行处理的时候,都要去找网上的资源,了解写法,很是麻烦。

于是自己写了一个方便自己使用的类,希望大家能够喜欢。

如果觉得还不够完善的,请自行修改源代码。


头文件:

//	#########################################################################
/**
@file			TraverseDir.h
@author			bluehe

@remarks		遍历文件目录

History :
                20150417  bluehe
                            创建
(C) 2015-2016 
*/
//	##########################################################################
/*
使用方法 :
	bool myCallBack(const char *FilePath,const char * FileName,bool bDir,void *CustomParam)
	{
		//找到一个符合条件的文件,并在下面进行自己的处理
	  ...

	  return true;	//true -- 继续遍历 false -- 立即停止
    }

	...

	//遍历c:/mypath下的所有文件和子目录
	TraverseDir TD("c:/mypath",TraverseDir::TM_Both,myCallBack,true,"*.*",NULL);
	TD.Do();

	//遍历c:/mypath下的所有文件,不遍历目录
	TraverseDir TD("c:/mypath",TraverseDir::TM_File,myCallBack,false,"*.*",NULL);
	TD.Do();

	//遍历c:/mypath下的所有mk开头的文件和目录
	TraverseDir TD("c:/mypath",TraverseDir::TM_Path,myCallBack,true,"mk*.*",NULL);
	TD.Do();

	//遍历c:/mypath下的所有扩展名为jpg的文件和目录,并将myobj对象传递到myCallBack,进行自定义处理
	TraverseDir TD("c:/mypath",TraverseDir::TM_File,myCallBack,true,"*.jpg",&myobj);
	TD.Do();


*/
//##########################################################################
#pragma once

#include <string>

struct TraverseDir
{
	enum enumTargetMode
	{
		TM_Dir = 0x0001,
		TM_File= 0x0002,
		TM_Both= TM_Dir | TM_File,
	};

	//找到文件后的回调处理
	//FilePath - 文件路径
	//FileName - 文件名称
	//bDir     - 是否为目录
	//CustomParam - 传递的自定义对象
	typedef bool (*Func )(const char *FilePath,const char * FileName,bool bDir,void *CustomParam);

	//设定搜索条件
	//BasePathSpec - 基础的路径描述
	//Mode         - 查找模式
	//F            - 指定处理文件的回调函数
	//bTraverseSub - 是否遍历子目录
	//FileSpec     - 文件描述
	//CustomParam  - 传递的自定义对象
	TraverseDir(const char * BasePathSpec,enumTargetMode Mode,Func F,bool bTraverseSub = true,const char *FileSpec = NULL,void* CustomParam = NULL);

	//执行搜索
	void Do();

protected:
	void _Do(const char * PathSpec);

	Func m_Func;
	std::string m_BasePathSpec;
	std::string m_FileSpec;
	bool m_bTraverseSub;
	enumTargetMode m_Mode;

	bool m_bStopTraverse;

	void * m_CustomParam;



};

实现文件:

#include "stdafx.h"
#include "TraverseDir.h"
#include <io.h>

TraverseDir::TraverseDir(const char * PathSpec,enumTargetMode Mode,Func F,bool bTraverseSub,const char * FileSpec,void *CustomParam)
		:  m_Func(F),m_bTraverseSub(bTraverseSub),m_Mode(Mode),m_bStopTraverse(false),m_CustomParam(CustomParam)
{
	m_BasePathSpec = PathSpec;
	if(!m_BasePathSpec.empty())
	{
		if(*m_BasePathSpec.rbegin() != '/' && *m_BasePathSpec.rbegin() != '\\')
			m_BasePathSpec += "/";
	}
	else
		m_BasePathSpec += "/";

	if(FileSpec)
		m_FileSpec = FileSpec;
	else
		m_FileSpec = "*.*";
}

void TraverseDir::_Do(const char *PathSpec)
{
	struct _finddata_t c_file;
	intptr_t hFile;

	std::string FullSpec;
	FullSpec = std::string(PathSpec) + m_FileSpec;

	hFile = _findfirst(FullSpec.c_str(),&c_file);
	if(hFile != -1L)
	{
		std::string TargetName;
		do 
		{
			if(std::string(c_file.name) != "."
				&& std::string(c_file.name) != "..")
			{
				TargetName = std::string(PathSpec) + c_file.name;

				if( c_file.attrib & _A_SUBDIR)
				{

					if(m_Mode & TM_Dir)
					{
						if(! m_Func(PathSpec,c_file.name,true,m_CustomParam))
						{
							m_bStopTraverse = true;
							break;
						}
					}


					TargetName += '/';
					_Do(TargetName.c_str());
					if(m_bStopTraverse)
						break;
				}
				else
				{
					if(m_Mode & TM_File)
					{
						if(! m_Func(PathSpec,c_file.name,false,m_CustomParam))
						{
							m_bStopTraverse = true;
							break;
						}
					}
				}
			}
		} while (_findnext(hFile,&c_file) == 0);

		_findclose(hFile);
	}
}

void TraverseDir::Do()
{
	m_bStopTraverse = false;
	if(m_Func == NULL)
		return;

	_Do(m_BasePathSpec.c_str());
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值