MFC选择文件目录查找带有指定后缀的所有文件

来源:互联网 发布:vb.net 用户控件 编辑:程序博客网 时间:2024/05/01 11:38

//选择目录文件 

void CDlg::OnBnClickedButtonChoosepath()

{    
CString m_strFilePath;
char szSelected[MAX_PATH];         //用来存放文件夹路径
BROWSEINFO bi;
LPITEMIDLIST pidl;
bi.hwndOwner = this->m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szSelected;
bi.lpszTitle = "选择查找路径";
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = NULL;
bi.iImage = NULL;
if ((pidl = SHBrowseForFolder(&bi)) != NULL)
{
if (SUCCEEDED(SHGetPathFromIDList(pidl, szSelected)))
{
m_strFilePath = szSelected;
}
}
FindFile(m_strFilePath);                      //查找当前目录文件下所有含有*.*文件

}


void CDlg::FindFile(CString& strDir)
{
if (strDir == (""))
{
return;
}
else
{
if (strDir.Right(1) != "\\")                        //使用的是多字节字符集
{
strDir += "\\";
}
strDir += "*.mp3*";                               //这里以查找后缀带.mp3为例
}
CFileFind finder;
CString strPath;
BOOL bWorking = finder.FindFile(strDir);
while (bWorking)
{
bWorking = finder.FindNextFile();
strPath = finder.GetFilePath();
if (finder.IsDirectory() && !finder.IsDots())
FindFile(strPath);                                        //递归调用
else if (!finder.IsDirectory() && !finder.IsDots())
{
                       //strPath就是查找带有.mp3后缀的文件路径

                      //你需要操作的部分
}
}
}

1 0
原创粉丝点击