19,469
社区成员
发帖
与我相关
我的任务
分享
#include <io.h>
//查找dir路径下所有文件(dir包括配置符,如c:\*.txt)
//ishavedir表示返回的文件名是否包含全路径,true为返回全路径的文件名,false只返回文件名
vector<string> FindAllFile(const char* dir,bool ishavedir=false)
{
_finddata_t file;
vector<string> file_list;
long lf;
if((lf = _findfirst(dir, &file))==-1l) { //_findfirst返回的是long型; long __cdecl _findfirst(const char *, struct _finddata_t *)
return file_list;
} else {
do {
if (ishavedir) {
string tmppath=dir;
int index=tmppath.find_last_of("*.")-1;
tmppath=tmppath.substr(0,index)+file.name;
file_list.push_back(tmppath);
} else {
file_list.push_back(file.name);
}
} while (_findnext( lf, &file ) == 0);//int _findnext(long, struct _finddata_t *);如果找到下个文件的名字成功的话就返回0,否则返回-1
_findclose(lf);
return file_list;
}
}
void ProcPicDir(CString strPicDir)
{
CFileFind fileFinder;
if (strPicDir.Right(1) == TEXT("\\"))
{
int nPos = strPicDir.ReverseFind(TEXT('\\'));
strPicDir = strPicDir.Left(nPos);
}
CString strPicFile = TEXT("");
strPicFile.Format(TEXT("%s\\%s"),strPicDir,TEXT("*.*"));
BOOL bWorking = fileFinder.FindFile(strPicFile);
while (bWorking)
{
bWorking = fileFinder.FindNextFile();
if (fileFinder.IsDots())
{
continue;
}
CString strFilePath = fileFinder.GetFilePath();
if (fileFinder.IsDirectory())
{
//继续遍历目录
ProcPicDir(strFilePath);
}
else
{
int nPos = strFilePath.ReverseFind(TEXT('.'));
CString strExt = strFilePath.Right(strFilePath.GetLength() - nPos - 1);
if (strExt.CompareNoCase(TEXT("jpg")) == 0 ||
strExt.CompareNoCase(TEXT("jpeg")) == 0 ||
strExt.CompareNoCase(TEXT("bmp")) == 0)
{
//处理图片
//...
}
}
}
fileFinder.Close();
}