windows 下面 查找一个文件夹下的所有文件。整理版

//----------第一种方法,可以再vc6.0上直接运行---------------------------------------------

#include <AFX.H>

void FindFilesInOneFolder(const std::string folder_in, vector <string>& files_out)
{
// 0. Initialize
int q=0;
string lastStr;
CFileFind finder;

// build a string with wildcards
CString str_root(folder_in.c_str());
CString str=str_root+_T("\\*.*");
CString filename;

// 1. Start working for files
BOOL bWorking = finder.FindFile(str);

while (bWorking)
{
// 2.
bWorking = finder.FindNextFile();

// 3. skip . and .. files; 
if (finder.IsDots())
continue;

// 4. if it's a directory, go inside it
if (finder.IsDirectory())
{
/*FindFilesInOneFolder((LPCTSTR)(finder.GetFilePath()),files_out);*/
continue;
}
else
// 5. Get filename
{
filename=finder.GetFilePath();
string ss=filename.GetBuffer(0);

q=ss.find_last_of('.');
lastStr=ss.substr(q,ss.size()-q);
if ((lastStr==".jpg") ||(lastStr==".bmp"))
{
files_out.push_back(ss);
}

}

}

// 6. Finished
finder.Close();
// return;

}


//----------第二种方法,可在vs2008上直接运行---------------------------------------------

#include <Windows.h>

void FindFilesInOneFolder(const std::string lpPath, std::vector <std::string>& files_out)
{

WIN32_FIND_DATA FindFileData;
HANDLE hFind=NULL;


string filePathName;
string fullPathName;


filePathName=lpPath+"\\*.*";
 
hFind=FindFirstFile(filePathName.data(),&FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
return;
do 
{
if ( "." == (string)FindFileData.cFileName || ".." == (string)FindFileData.cFileName )
{
continue;
}


fullPathName=lpPath+"\\"+FindFileData.cFileName;
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
/*FindFilesInOneFolder(fullPathName,files_out);*/
continue;
}
else
{
string str_tmp=fullPathName.substr(fullPathName.length()-3);
if ("jpg"==str_tmp || "bmp"==str_tmp || "ppm"==str_tmp)
{
files_out.push_back(fullPathName);
}


}
} while (FindNextFile(hFind,&FindFileData));

    FindClose(hFind);


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值