int Find_special_File(LPCTSTR pszPath, //查找起始路径
LPCTSTR pszExt, //文件扩展名
BOOL include_subDirectory, //搜索子目录标志
CListBox *pList) //列表
{
int found_count = 0; //查找计数器
int file_fliter_len = CString(pszExt).GetLength(); //文件扩展名长度
CString target_file = pszPath; //目标路径
if(target_file.GetLength() > 1 && target_file.Right(1) != '\\')
target_file += '\\';
if(include_subDirectory) //包括子目录
target_file += "*.*";
else target_file += pszExt;
bool b_find_all_file = false; //查找所有文件指示
if(CString(pszExt) == ".*") //扩展名使用通配符
b_find_all_file = true;
//查找文件
CFileFind find;
BOOL ret = find.FindFile(target_file);
while(ret)
{
ret = find.FindNextFile();
if(find.IsDots()) //. 或 .. 文件
{
}
else if(find.IsDirectory()) //子目录
{
if(include_subDirectory) //搜索子目录标志
{
//递归方式搜索下一级目录
CString next_path = find.GetFilePath();
found_count += Find_special_File(next_path,pszExt,include_subDirectory,pList); }
}
else //file
{
CString file_Name = find.GetFilePath();
if(b_find_all_file //所有文件
|| ( file_Name.GetLength() > file_fliter_len && file_Name.Right(file_fliter_len) == pszExt) //扩展名匹配
|| 0)
{
pList->AddString(file_Name); //插入列表
found_count++; //搜索计数增加
}
}
}
find.Close(); //关闭搜索
return(found_count); //返回搜索到数目
}
递归方式搜索文件
最新推荐文章于 2024-07-25 11:55:21 发布