文件夹所有文件的拷贝SHFileOperation

时间:2020-12-17 21:37:52
//帮我看一看这段代码有什么错误,提示“无法读原文件或磁盘”错误
//功能为:从一个文件夹拷贝所有文件到另一个文件夹
void CFilecopyDialog::OnButton4() 
{

HWND m_hWnd;
m_hWnd=GetSafeHwnd();
int nOk;
//m_srcpath和m_despath为edit编辑框变量,取得的是文件夹名称
CString s_srcpath,s_despath;
s_srcpath=m_srcpath+"\\*.*";//原文件夹的所有文件
s_despath=m_despath;//目的文件夹
//设置原路径
char *strSrc;
strSrc=(LPSTR)(LPCTSTR)s_srcpath;
//设置目的路径
char  *strDst;
 strDst=(LPSTR)(LPCTSTR)s_despath;//
 char  strTitle[200]="正在拷贝文件";//进度题头
     SHFILEOPSTRUCT FileOp;
     FileOp.hwnd=m_hWnd;
     FileOp.wFunc=FO_COPY;
     FileOp.pFrom=strSrc;
     FileOp.pTo=strDst;
     FileOp.fFlags=FOF_ALLOWUNDO;
     FileOp.hNameMappings=NULL;
     FileOp.lpszProgressTitle=strTitle;
 nOk=SHFileOperation(&FileOp);
     if(nOk)
          TRACE("There is an error: %d\n",nOk);
     else
          TRACE("SHFileOperation  finished successfully\n");
 if(FileOp.fAnyOperationsAborted)
  TRACE("Operation was aborted!\n");

}

5 个解决方案

#1


char a[100];
ZeroMemory(a,100);
strcpy(a,(LPCSTR)srcpath);
FileOp.pFrom = a;
我刚刚解决这个问题,用字符常量是不行的,cstring也是
间接利用LPCSTR转换,可能是文件名后面必须有两个0结束符。

#2


CString 的重载LPCSTR的,所以你只要(LPCSTR)就可以了

#3


char buf[256];
char buf1[256];
SHFILEOPSTRUCT fo;

memset(buf, 0, sizeof(buf));
memset(buf1, 0, sizeof(buf1));
memset(&fo, 0, sizeof(fo));
CString str1 = "f:\\source", str2 = "f:\\target";
strcpy(buf, str1);
strcpy(buf1, str2);

fo.wFunc = FO_MOVE;//复制是FO_COPY,删除是FO_DELETE;
fo.pFrom = buf;
fo.pTo = buf1;
fo.fFlags = FOF_SIMPLEPROGRESS;
SHFileOperation(&fo);

#4


SHFILEOPSTRUCT  Op;

char FromBuf[]="E:\\temp\0";
char ToBuf[]="E:\\个人文档\\Crystal521\0";;

Op.hwnd = NULL;
Op.wFunc = FO_COPY;
Op.pFrom = FromBuf; 
Op.pTo = ToBuf; 
Op.fFlags = FOF_NOCONFIRMATION | FOF_RENAMEONCOLLISION ; 
Op.fAnyOperationsAborted = FALSE; 
Op.hNameMappings = NULL; 
Op.lpszProgressTitle = NULL;

if(SHFileOperation(&Op) == 0)
MessageBox("复制完毕","提示",MB_OK|MB_ICONINFORMATION);

#5


char  From[100]={0};
strcpy(From,m_strSourcename.GetBuffer(m_strSourcename.GetLength()));
char To[100]={0};
strcpy(To,strDestname.GetBuffer(strDestname.GetLength()));
…………………………………………………………
注意是要两个"\0" 


#1


char a[100];
ZeroMemory(a,100);
strcpy(a,(LPCSTR)srcpath);
FileOp.pFrom = a;
我刚刚解决这个问题,用字符常量是不行的,cstring也是
间接利用LPCSTR转换,可能是文件名后面必须有两个0结束符。

#2


CString 的重载LPCSTR的,所以你只要(LPCSTR)就可以了

#3


char buf[256];
char buf1[256];
SHFILEOPSTRUCT fo;

memset(buf, 0, sizeof(buf));
memset(buf1, 0, sizeof(buf1));
memset(&fo, 0, sizeof(fo));
CString str1 = "f:\\source", str2 = "f:\\target";
strcpy(buf, str1);
strcpy(buf1, str2);

fo.wFunc = FO_MOVE;//复制是FO_COPY,删除是FO_DELETE;
fo.pFrom = buf;
fo.pTo = buf1;
fo.fFlags = FOF_SIMPLEPROGRESS;
SHFileOperation(&fo);

#4


SHFILEOPSTRUCT  Op;

char FromBuf[]="E:\\temp\0";
char ToBuf[]="E:\\个人文档\\Crystal521\0";;

Op.hwnd = NULL;
Op.wFunc = FO_COPY;
Op.pFrom = FromBuf; 
Op.pTo = ToBuf; 
Op.fFlags = FOF_NOCONFIRMATION | FOF_RENAMEONCOLLISION ; 
Op.fAnyOperationsAborted = FALSE; 
Op.hNameMappings = NULL; 
Op.lpszProgressTitle = NULL;

if(SHFileOperation(&Op) == 0)
MessageBox("复制完毕","提示",MB_OK|MB_ICONINFORMATION);

#5


char  From[100]={0};
strcpy(From,m_strSourcename.GetBuffer(m_strSourcename.GetLength()));
char To[100]={0};
strcpy(To,strDestname.GetBuffer(strDestname.GetLength()));
…………………………………………………………
注意是要两个"\0"