MFC 目录选取,指定类型文件查找复制
2013-10-06 22:32
453 查看
目录选取:
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = m_hWnd;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpszTitle = _T("Browse A Input Path");
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
BOOL bRet = FALSE;
TCHAR szFolder[MAX_PATH*2];
szFolder[0] = _T('/0');
if (pidl)
{
if (SHGetPathFromIDList(pidl, szFolder))
bRet = TRUE;
IMalloc *pMalloc = NULL;
if (SUCCEEDED(SHGetMalloc(&pMalloc)) && pMalloc)
{
pMalloc->Free(pidl);
pMalloc->Release();
}
}
if(bRet)
{
m_strInputPath = szFolder;//选择的文件夹路径
SetDlgItemText(IDC_EINPUT,m_strInputPath);
}
/////////////////////////////////////////////////////////////////////////////////////////////
递归查找指定类型的文件:
void CFileFilterToolDlg::Recurse(CString strDir,CString strExt)
{
CFileFind finder;
CString strCurrDir;
strCurrDir = strDir + _T("\\*.*");
BOOL bWorking = finder.FindFile(strCurrDir);
while(bWorking)
{
bWorking = finder.FindNextFile();
CString tempFileName = finder.GetFilePath();
//cout<< (LPCTSTR) temp<<endl;
if(finder.IsDots())continue;
if(finder.IsDirectory())
{
CString str = finder.GetFilePath();
// cout << (LPCTSTR) str << endl;
Recurse(str,strExt);
}
else
{
//MessageBox(tempFileName);
CString strCurrFileExt = tempFileName.Right(tempFileName.GetLength() - tempFileName.ReverseFind(_T('.')));
//MessageBox(strCurrFileExt);
CString strCurrFileName = tempFileName.Right(tempFileName.GetLength() - tempFileName.ReverseFind(_T('\\')) - 1);
//MessageBox(strCurrFileName);
CString strDstFileName = m_strOutputPath + _T("\\") + strCurrFileName;
//MessageBox(strDstFileName);
if(0 == strCurrFileExt.Compare(strExt))
{
int nRet = MCopyFile(tempFileName,strDstFileName,this->m_bCoverWrite);
CString strMsg = strCurrFileName;
if(0 == nRet)
{
m_nCountFindFilsCopySuccess++;
strMsg += _T(" :Success");
}
else if(-1 == nRet)
{
CString str;
str.Format(_T("复制文件%s时失败,该文件已存在!"),tempFileName);
//MessageBox(str);
strMsg += _T(" :") + str;
}
m_ctrlListFiles.InsertString(m_nCountTotalFilesFinded,strMsg);
m_ctrlListFiles.SetCurSel(m_nCountTotalFilesFinded);
m_nCountTotalFilesFinded++;
}
//MessageBox(strCurrFileExt);
}
}
finder.Close();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//复制该文件
PathFileExists的头文件和库:
#include <shlwapi.h>
#pragma comment(lib,"Shlwapi.lib")
int MCopyFile(CString strSrc, CString strDest, BOOL cover)
{
if(!cover)
{
if(PathFileExists(strDest))return -1;
}
//CopyFile((LPCSTR)strSrc, (LPCSTR)strDest, cover);
CopyFile(strSrc, strDest, cover);
return 0;
}
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = m_hWnd;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpszTitle = _T("Browse A Input Path");
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
BOOL bRet = FALSE;
TCHAR szFolder[MAX_PATH*2];
szFolder[0] = _T('/0');
if (pidl)
{
if (SHGetPathFromIDList(pidl, szFolder))
bRet = TRUE;
IMalloc *pMalloc = NULL;
if (SUCCEEDED(SHGetMalloc(&pMalloc)) && pMalloc)
{
pMalloc->Free(pidl);
pMalloc->Release();
}
}
if(bRet)
{
m_strInputPath = szFolder;//选择的文件夹路径
SetDlgItemText(IDC_EINPUT,m_strInputPath);
}
/////////////////////////////////////////////////////////////////////////////////////////////
递归查找指定类型的文件:
void CFileFilterToolDlg::Recurse(CString strDir,CString strExt)
{
CFileFind finder;
CString strCurrDir;
strCurrDir = strDir + _T("\\*.*");
BOOL bWorking = finder.FindFile(strCurrDir);
while(bWorking)
{
bWorking = finder.FindNextFile();
CString tempFileName = finder.GetFilePath();
//cout<< (LPCTSTR) temp<<endl;
if(finder.IsDots())continue;
if(finder.IsDirectory())
{
CString str = finder.GetFilePath();
// cout << (LPCTSTR) str << endl;
Recurse(str,strExt);
}
else
{
//MessageBox(tempFileName);
CString strCurrFileExt = tempFileName.Right(tempFileName.GetLength() - tempFileName.ReverseFind(_T('.')));
//MessageBox(strCurrFileExt);
CString strCurrFileName = tempFileName.Right(tempFileName.GetLength() - tempFileName.ReverseFind(_T('\\')) - 1);
//MessageBox(strCurrFileName);
CString strDstFileName = m_strOutputPath + _T("\\") + strCurrFileName;
//MessageBox(strDstFileName);
if(0 == strCurrFileExt.Compare(strExt))
{
int nRet = MCopyFile(tempFileName,strDstFileName,this->m_bCoverWrite);
CString strMsg = strCurrFileName;
if(0 == nRet)
{
m_nCountFindFilsCopySuccess++;
strMsg += _T(" :Success");
}
else if(-1 == nRet)
{
CString str;
str.Format(_T("复制文件%s时失败,该文件已存在!"),tempFileName);
//MessageBox(str);
strMsg += _T(" :") + str;
}
m_ctrlListFiles.InsertString(m_nCountTotalFilesFinded,strMsg);
m_ctrlListFiles.SetCurSel(m_nCountTotalFilesFinded);
m_nCountTotalFilesFinded++;
}
//MessageBox(strCurrFileExt);
}
}
finder.Close();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//复制该文件
PathFileExists的头文件和库:
#include <shlwapi.h>
#pragma comment(lib,"Shlwapi.lib")
int MCopyFile(CString strSrc, CString strDest, BOOL cover)
{
if(!cover)
{
if(PathFileExists(strDest))return -1;
}
//CopyFile((LPCSTR)strSrc, (LPCSTR)strDest, cover);
CopyFile(strSrc, strDest, cover);
return 0;
}
相关文章推荐
- MFC 遍历目录下指定类型的文件并复制
- 查找该目录下指定文件类型的用 vc 6.0 编写 的关于文件搜索的例子
- 根据提供的文件名文本,查找指定目录下的该文件,找到后复制到指定文件夹--bash
- linux 指定文件类型 全目录查找
- 在指定目录中查找指定类型的文件,并且获取文件名再显示
- MFC选择文件目录查找带有指定后缀的所有文件
- win32控制台下实现查找并显示指定目录下的指定类型文件
- c++查找目录下的指定类型的全部文件
- 批处理:查找指定条件的文件复制到指定的目录中
- grep命令在指定目录中的指定文件类型中查找关键字
- Linux复制指定目录及子目录下特定类型的文件
- grep如何递归目录并在指定类型文件中查找
- 目录下指定类型的文件复制到另外一个目录
- Linux复制指定目录及子目录下特定类型的文件
- 002_018 Python 查找目录中的第一个文件,指定类型
- C++及API函数实现查找某目录下指定类型的文件
- Linux下用find查找并复制指定文件到指定目录下
- ubuntu 查找含有特定字符的文件并复制到指定目录
- 在当前目录中所有文件中,查找,存在指定内容的指定文件类型
- 用grep查找指定目录及其子目录下的特定文件类型