//----------第一种方法,可以再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);
}