您的位置:首页 > 编程语言

使用MFC搜索当前目录下所存在的某种文件,将文件名(路径等)添加到下拉框中,返回文件个数,部分关键代码。。

2009-08-27 16:06 716 查看
//搜索当前目录下所存在的配置文件,返回文件个数
int CNewsPaperDlg::Find_special_File(LPCTSTR pszPath, //查找起始路径

LPCTSTR pszExt, //文件扩展名

BOOL include_subDirectory, //搜索子目录标志

CComboBox *pBox) //列表

{

//int found_count = 0;
m_iXmlCount = 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();
m_iXmlCount = Find_special_File(next_path,pszExt,
include_subDirectory,pBox);
}

}

else //file
{

CString file_Name = find.GetFilePath();
CString tempFileName;

if(b_find_all_file //所有文件

|| ( file_Name.GetLength() > file_fliter_len

&& file_Name.Right(file_fliter_len) == pszExt) //扩展名匹配

)
{
//先将绝对路径存储到一个数组当中
m_csXmlLocation[m_iXmlCount] = file_Name;

//解析出文件名,首先去掉后面的.xml或者.ini
file_Name = file_Name.Left( file_Name.GetLength() - 4);
tempFileName = file_Name;
int pos = 0, iTotalPos = 0;;
while (file_Name.GetLength() > 0)
{
iTotalPos += pos;
pos = file_Name.Find('//');
if(pos != -1)
{
file_Name = file_Name.Mid(pos + 1, file_Name.GetLength()- (pos + 1) );
}
//AfxMessageBox(file_Name);
else
{
break;
}

}

m_csXmlFileName[m_iXmlCount] = file_Name;

//然后将文件名放到下拉列表当中
pBox->AddString(file_Name); //插入列表

++m_iXmlCount; //搜索计数增加
}
}

}
find.Close(); //关闭搜索
return(m_iXmlCount); //返回搜索到数目

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐