在热心的网友帮助下,程序代码出来了。但是不能成功运行。帮忙调试一下,能运行但是不能完成任务。帮忙看看!程序的目的是把不同文件夹下的文件拷贝到指定的文件夹!void CMy11Dlg::OnOK()
{
// TODO: Add extra validation here
TCHAR pszPath[MAX_PATH];
BROWSEINFO bi;
bi.hwndOwner = this->GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = NULL;
bi.lpszTitle = _T("请选择文件夹");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl == NULL)
{
return;
}
CString strDst = "D:\\data\\";
CreateDirectory(strDst, NULL);
if (SHGetPathFromIDList(pidl, pszPath))
{
CFileFind finder;
CString strSrc;
strSrc.Format("%s\\*.xls",pszPath);//如果是所有文件将.xls改为*.*
BOOL bFind = finder.FindFile(strSrc);
while (bFind)
{
bFind = finder.FindNextFile();
if (finder.IsDots())
{
continue;
}
if (finder.IsDirectory())
{
continue;//如果pszPath中有目录,并目录中还有.xml则可递归find和copy
}
else
{
//拷贝文件
CString strDstFile = strDst + finder.GetFileName();//这里可以直接通过CString的"CString::operator +"来将两个字符串组合
if(!CopyFile(strSrc, strDstFile, FALSE))
{
MessageBox("拷贝成功");
}
}
}
finder.Close();
}
CDialog::OnOK();
}
mfc文件复制
{
// TODO: Add extra validation here
TCHAR pszPath[MAX_PATH];
BROWSEINFO bi;
bi.hwndOwner = this->GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = NULL;
bi.lpszTitle = _T("请选择文件夹");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl == NULL)
{
return;
}
CString strDst = "D:\\data\\";
CreateDirectory(strDst, NULL);
if (SHGetPathFromIDList(pidl, pszPath))
{
CFileFind finder;
CString strSrc;
strSrc.Format("%s\\*.xls",pszPath);//如果是所有文件将.xls改为*.*
BOOL bFind = finder.FindFile(strSrc);
while (bFind)
{
bFind = finder.FindNextFile();
if (finder.IsDots())
{
continue;
}
if (finder.IsDirectory())
{
continue;//如果pszPath中有目录,并目录中还有.xml则可递归find和copy
}
else
{
//拷贝文件
CString strDstFile = strDst + finder.GetFileName();//这里可以直接通过CString的"CString::operator +"来将两个字符串组合
if(!CopyFile(strSrc, strDstFile, FALSE))
{
MessageBox("拷贝成功");
}
}
}
finder.Close();
}
CDialog::OnOK();
}
mfc文件复制
解决方案 »
- 关于VC读取Excel颜色的问题
- 如何获得网卡的固有速率
- VC/MFC问题,建一个新类
- 如何固定ToolBar,让其停泊在Top,而不会被用户移动
- 重画问题!
- IHTMLFORMELEMENT::put_onsubmit(VARIANT v)如何使用
- 如何得到包含ActiveX控件的IE的URL
- @@@@@@@@@@@@@@CFtpConnection的getfile的问题@@@@@@@@@@@
- 如何销毁一个在其运行过程中阻塞的线程?
- mfc Activex程序 求助
- 运行到断点处时,程序却在另一个不同文件夹中的同名文件的相同位置中断
- 创建Activex控件,控件怎么是windowless,急的快哭了
copyfile(d:\a\b.txt, e:\a\b.txt); e:\a必须存在
bool Normalize( CString&path )
{
bool ret = !path.IsEmpty();
if (ret)
{
wchar_t slashes = path[path.GetLength()-1];
if (slashes != L'\\'
&& slashes != L'/')
{
if (path.Find(L'/') != -1){
path += L"/";
}
else{
path += L"\\";
}
}
path.Replace(L'/', L'\\');
path.Replace(L"\\\\", L"\\");
}
return ret;
} void CopyFolderAllFiles(CString csSourceFolder, CString csNewFolder, bool &result)
{
CStringsrc = csSourceFolder;
CStringdest = csNewFolder;
Normalize(src);
Normalize(dest);
CFileFindEx f;
BOOL bFind=f.FindFile(src+ L"*.*");
while(bFind)
{
bFind = f.FindNextFile();
if(f.IsDots())
continue; CStringname = f.GetFileName();
if(f.IsDirectory())
{
if(!::CreateDirectory(dest+name, NULL)){
result = false;
break;
}
else{
CopyFolderAllFiles(src+name,dest+name, result);
}
}
else
{
::SetFileAttributes(src+name,FILE_ATTRIBUTE_NORMAL);
if(!::CopyFile(src+name,dest+ name,FALSE))
{
result = false;
break;
}
}
}
}