使用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); //返回搜索到数目
}
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); //返回搜索到数目
}
相关文章推荐
- 【代码】当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出绝对路径
- MFC 获取可执行文件的路径 获取当前可执行文件的目录
- [Object C]_[初级]_[创建文件路径时,已存在同名目录和文件名的问题的解决方案]
- 搜索当前目录下所有文件包含某关键词的文本/文件名/行号
- java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里
- asp.net中使用ckfinder2选择图片并返回图片文件的路径的代码
- 获取任意文件路径及文件名、运行程序当前目录
- Python:编写函数,能在当前目录以及子目录下查找文件名包含指定字符串的文件,并打印出完整路径
- JavaScript获取当前文件全路径、当前目录、当前文件名
- JavaScript获取当前文件全路径、当前目录、当前文件名
- Python 在当前目录以及其子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
- 分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里
- SpringMVC请求使用@PathVariable获取文件名称并且文件名中存在.导致路径被截取的问题
- 怎样使用 Python 来判断一个路径是否存在判断一个路径是文件还是目录
- matlab中当前文件目录和搜索路径的区别
- 使用grep指令在当前目录下所有文件中搜索指定文本
- PHP dirname() 返回路径中的目录部分basename() 函数返回路径中的文件名部分。
- 添加数据文件时使用已存在的文件名的方法 ORA-00933
- Python搜索目录下指定的文件,并返回绝对路径(包括子目录)
- 编写一个Python程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径