删除:
void COperationDlg::OnDel2(CString m_strFileDictory) //参数就是目录的路径
{
if(m_strFileDictory.GetLength()==0)
{
::AfxMessageBox (“目录名非法!”,MB_OK|MB_ICONEXCLAMATION);
return;
}
char FromFileName[80]=”/0″;
strcpy(FromFileName,m_strFileDictory);
strcat(FromFileName,”/0″);
SHFILEOPSTRUCT lpFileOp;
lpFileOp.hwnd =GetSafeHwnd();
lpFileOp.wFunc =FO_Delete;
lpFileOp.pFrom =FromFileName;
lpFileOp.pTo=NULL;
lpFileOp.fFlags =FOF_SILENT|FOF_NOCONFIRMATION;
lpFileOp.fAnyOperationsAborted =FALSE;
lpFileOp.hNameMappings =NULL;
lpFileOp.lpszProgressTitle =NULL;
int rval=SHFileOperation(&lpFileOp);
if(rval==0)
{
if(lpFileOp.fAnyOperationsAborted ==TRUE)
::AfxMessageBox (“删除目录操作取消”,MB_OK);
else
::AfxMessageBox(“删除目录操作成功!”,MB_OK|MB_ICONEXCLAMATION);
}
else
{
::AfxMessageBox (“删除目录操作失败!”,MB_OK|MB_ICONEXCLAMATION);
}
}
拷贝
BOOL CCutfoldDlg::CopyDirectory(CString SourcePath,CString CopytoPath)
{
CFileFind tempFind;
char tempFileFind[200];
char tempFileFind1[200];
SECURITY_ATTRIBUTES lpSecurityDescriptor;
CreateDirectory(CopytoPath,&lpSecurityDescriptor);
/* {
if(::MessageBox(0,”文件已经存在,是否覆盖?”,”警告”,MB_ICONQUESTION|MB_YESNO)==IDNO)
return FALSE;
}
*/
sprintf(tempFileFind1,”%s//*.*”,CopytoPath);
sprintf(tempFileFind,”%s//*.*”,SourcePath);
BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
// BOOL IsFinded1=(BOOL)tempFind.FindFile(tempFileFind1);
while(IsFinded)
{
IsFinded=(BOOL)tempFind.FindNextFile();
if(!tempFind.IsDots())
{
char foundFileName[200];
// char foundFileName1[200];
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
// strcpy(foundFileName1,tempFind.GetFileName().GetBuffer(200));
if(tempFind.IsDirectory())
{
char tempDir[200];
char tempDir1[200];
sprintf(tempDir,”%s//%s”,SourcePath,foundFileName);
sprintf(tempDir1,”%s//%s”,CopytoPath,foundFileName);
CopyDirectory(tempDir,tempDir1);
}
else
{
char tempFileName[200];
char tempFileName1[200];
sprintf(tempFileName,”%s//%s”,SourcePath,foundFileName);
sprintf(tempFileName1,”%s//%s”,CopytoPath,foundFileName);
//DeleteFile(tempFileName);
CopyFile(tempFileName,tempFileName1,0);
}
}
}
tempFind.Close();
return TRUE;
}
移动
要实现移动,遍历文件+MoveFile+CreateDirectory
遍历方法参考:
HANDLE hFind;
WIN32_FIND_DATA dataFind;
BOOL bMoreFiles=TRUE;
hFind=FindFirstFile(sPath+”//*.txt”,&dataFind);////sPath表示路径
while(hFind!=INVALID_HANDLE_VALUE && bMoreFiles==TRUE)
{
if(dataFind.dwFileAttributes==FILE_ATTRIBUTE_ARCHIVE)
{
MessageBox(dataFind.cFileName);
}
bMoreFiles=FindNextFile(hFind,&dataFind);
}
FindClose(hFind);