#pragma once
//枚举指定路径下的制定类型文件
//@LPCTSTR 文件全路径
//@pUserData 用户参数数据?
//@return 是否是用户取消
typedef BOOL (CALLBACK * LookUpFileCallBack)(LPCTSTR/*文件名称(包含全路径)*/);
#ifdef COMDLL_EXPORTS
#define COMDLL_API __declspec(dllexport)
#else
#define COMDLL_API __declspec(dllimport)
#endif
class COMDLL_API CEnumFile //导入类
{
public:
CEnumFile(LPCTSTR lpszPath,LPCTSTR lpszFileExt, LookUpFileCallBack pCallBack);
//回调获取文件的全路径信息
//@lpszPath 目录名称
//@lpszFileExt 指定的文件扩展名
//@bRecursion 递归,循环;递归式
//@bEnumFiles 是否迭代枚举所有文件
//@pCallBack 回调函数,每一个文件调用一次
//@pUserData ???用户参数
//调用方式:CEnumFile::CEnumFile(CGlobalData::strCurrPath + _T("LNG\\"),_T(".lng"),EnumLngFile);
CEnumFile(LPCTSTR lpszPath,LPCTSTR lpszFileExt,LookUpFileCallBack pCallBack,BOOL bRecursion/*=FALSE*/, BOOL bEnumFiles/*=TRUE*/);
public:
~CEnumFile(void);
};
#include "stdafx.h"
#include "EnumFile.h"
#include <windows.h>
CEnumFile::CEnumFile(LPCTSTR lpszPath,LPCTSTR lpszFileExt,LookUpFileCallBack pCallBack)
{
::CEnumFile(lpszPath,lpszFileExt,pCallBack,FALSE,FALSE);
}
CEnumFile::CEnumFile(LPCTSTR lpPath,LPCTSTR lpszFileExt,LookUpFileCallBack pCallBack,BOOL bRecursion, BOOL bEnumFiles)
{
static BOOL s_bUserBreak = FALSE;
try{
//-------------------------------------------------------------------------
if(s_bUserBreak) return;
int len = lstrlen(lpPath);
if(lpPath==NULL || len<=0) return;
//NotifySys(NRS_DO_EVENTS, 0,0);
CString path=_T("");
path.Format(_T("%s"),lpPath);
//if (str.Right(1)!='\\')//一种判断
if(path.ReverseFind('\\')!=1)//二种判断
path.Format(path+"%s","\\");
path.Format(path+"%s","*");
/*char path[MAX_PATH];
strcpy(path, (const char*)lpPath);
if(lpPath[len-1] != '\\')
strcat(path, "\\");
strcat(path, "*");*/
WIN32_FIND_DATA fd;
HANDLE hFindFile = FindFirstFile((LPCTSTR)path, &fd);
if(hFindFile == INVALID_HANDLE_VALUE)
{
::FindClose(hFindFile);
return;
}
CString tempPath;
BOOL bUserReture=TRUE;
BOOL bIsDirectory;
BOOL bFinish = FALSE;
while(!bFinish)
{
tempPath.Format(_T("%s"),lpPath);
if(path.ReverseFind('\\')!=1)//二种判断
path.Format(path+"%s","\\");
path.Format(path+"%s",fd.cFileName);
bIsDirectory = ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
//如果是.或..
CString s1;
s1.Format(_T("%s"),fd.cFileName);
if( bIsDirectory && (s1.CompareNoCase(_T("."))==0 || s1.CompareNoCase(_T(".."))==0))
{
bFinish = (FindNextFile(hFindFile, &fd) == FALSE);
continue;
}
if(pCallBack && bEnumFiles!=bIsDirectory)
{
if(lpszFileExt!=NULL)//判断,查找指定扩展名的文件
{
int idx=s1.ReverseFind('.');
if (-1!=idx)
{
CString tmp=s1.Right(lstrlen(s1)-idx);
if(tmp.CompareNoCase(lpszFileExt)==0)
{
bUserReture = pCallBack(tempPath+s1);
if(bUserReture==FALSE)
{
s_bUserBreak = TRUE;
::FindClose(hFindFile);
return;
}
}
}
/*char* pdot;
char* ch = new char[tempPath.GetLength()+1];
//
char szStr[MAX_PATH] = {0};
wcstombs(szStr, tempPath, tempPath.GetLength());
const char * p = szStr;
//UNICODE下宽字符的CString转换为const char * http://www.cnblogs.com/sunnyjones/archive/2009/02/24/1397529.html
strncpy(ch, p, tempPath.GetLength()+1);
//tempPath.GetBuffer(tempPath.GetLength()+1);
if((pdot = strrchr(ch, '.')) && stricmp(pdot, (const char *)lpszFileExt) == 0)
{
bUserReture = pCallBack(tempPath);
if(bUserReture==FALSE)
{
s_bUserBreak = TRUE;
::FindClose(hFindFile);
return;
}
}*/
}
else//没有限定扩展名
{
bUserReture = pCallBack(tempPath);
if(bUserReture==FALSE)
{
s_bUserBreak = TRUE;
::FindClose(hFindFile);
return;
}
}
}
//NotifySys(NRS_DO_EVENTS, 0,0);
if(bIsDirectory && bRecursion) //是子目录
{
CEnumFile(tempPath, lpszFileExt, pCallBack, bRecursion, bEnumFiles);
}
bFinish = (FindNextFile(hFindFile, &fd) == FALSE);
}
::FindClose(hFindFile);
//-------------------------------------------------------------------------
}catch(...)
{
ASSERT(0);
return;
}
}
CEnumFile::~CEnumFile()
{
}
我的项目中,vc++下需要迭代出指定目录下指定扩展名的文件的工具类