void filesInDir(string filedir, vector<string> &file, string filetype )
{
WIN32_FIND_DATA findData;
HANDLE handle;
string allFilePath = filedir + "\\*.*";
handle = FindFirstFile( allFilePath.c_str(), &findData );
if( handle == INVALID_HANDLE_VALUE ) return;
while (::FindNextFile(handle,&findData) )
{
if( strcmp(findData.cFileName,".") == 0 || strcmp(findData.cFileName,"..") == 0 ) continue;
string curfileordir = filedir + "\\" + findData.cFileName;
if ( findData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
{
filesInDir(curfileordir,file,filetype);
}
else
{
string filename = findData.cFileName;
int pos = filename.find_last_of(".");
string curtype = "*"+filename.substr( pos );
if ( strcmp(filetype.c_str(),curtype.c_str()) == 0 )
{
file.push_back( curfileordir );
}
}
}
{
WIN32_FIND_DATA findData;
HANDLE handle;
string allFilePath = filedir + "\\*.*";
handle = FindFirstFile( allFilePath.c_str(), &findData );
if( handle == INVALID_HANDLE_VALUE ) return;
while (::FindNextFile(handle,&findData) )
{
if( strcmp(findData.cFileName,".") == 0 || strcmp(findData.cFileName,"..") == 0 ) continue;
string curfileordir = filedir + "\\" + findData.cFileName;
if ( findData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
{
filesInDir(curfileordir,file,filetype);
}
else
{
string filename = findData.cFileName;
int pos = filename.find_last_of(".");
string curtype = "*"+filename.substr( pos );
if ( strcmp(filetype.c_str(),curtype.c_str()) == 0 )
{
file.push_back( curfileordir );
}
}
}
}
要包含头文件:#include <windows.h>
编译环境:windows vs2012