1.删除非空目录
//删除文件夹目录(非空)
bool DeleteDirectory(char* sDirName)
{
CFileFind tempFind;
char sTempFileFind[200] ;
sprintf(sTempFileFind,"%s/*.*",sDirName);
BOOL IsFinded = tempFind.FindFile(sTempFileFind);
while (IsFinded)
{
IsFinded = tempFind.FindNextFile();
if (!tempFind.IsDots())
{
char sFoundFileName[200];
strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200));
if (tempFind.IsDirectory())
{
char sTempDir[200];
sprintf(sTempDir,"%s/%s",sDirName,sFoundFileName);
DeleteDirectory(sTempDir); //先删除文件夹下边的所有文件
}
else
{
char sTempFileName[200];
sprintf(sTempFileName,"%s/%s",sDirName,sFoundFileName);
DeleteFile(sTempFileName); //将该文件夹删除
}
}
}
tempFind.Close();
if(!RemoveDirectory(sDirName))
{
return FALSE;
}
return TRUE;
}
2. 文件拷贝
void FileCopyTo(CString source, CString destination, CString searchStr, BOOL cover = TRUE)
{
CString strSourcePath = source;
CString strDesPath = destination;
CString strFileName = searchStr;
CFileFind filefinder;
CString strSearchPath = strSourcePath + "//" + strFileName;
CString filename;
BOOL bfind = filefinder.FindFile(strSearchPath);
CString SourcePath, DisPath;
while (bfind)
{
bfind = filefinder.FindNextFile();
filename = filefinder.GetFileName();
SourcePath = strSourcePath + "//" + filename;
DisPath = strDesPath + "//" + filename;
CopyFile((LPCSTR)SourcePath, (LPCSTR)DisPath, cover);
}
filefinder.Close();
-------------------------------------------------------------------------------------------------------------------------------
VC 复制移动删除重命名文件文件夹
━━━━━━━━━━━━━━━━━━━━━━━━
说明:
1.以下封装了4个函数_CopyFile,_DeleteFile,_MoveFile,_ReNameFile
2.每个函数都先拷贝了原来的路径,保证了路径是以2个/0/0结尾。(如果不以2个/0/0结尾,往往会出错)
3.对于文件或文件夹都适用
/* 能产生对话框的标志: FOF_SILENT //progress dialog FOF_RENAMEONCOLLISION //replace dialog FOF_NOCONFIRMATION //confirmation dialog FOF_NOCONFIRMMKDIR //asks for your permission to create a new folder FOF_NOERRORUI //error message
FOF_ALLOWUNDO //允许放回回收站,一般这个最好做默认 FOF_NOCONFIRMATION //不出现确认对话框(当需要覆盖时) FOF_SILENT //不显示进度对话框 FOF_NOERRORUI //No user interface will be displayed if an error occurs. FOF_NOCONFIRMMKDIR //如果目的目录不存在,就默认创建,不需确认 FOF_RENAMEONCOLLISION //如果文件(夹)已存在,自动为其添加序列号修改文件名。此时不会出现确认对话框 FOF_MULTIDESTFILES //把文件夹里的所有内容复制到FileOp.pTo指定的文件夹里,而不是直接复制一个文件夹 FOF_SIMPLEPROGRESS //Displays a progress dialog box but does not show the file names FOF_CONFIRMMOUSE //Not currently implemented. FOF_FILESONLY: //执行通配符,只执行文件; FOF_WANTMAPPINGHANDLE: //要求SHFileOperation()函数返回正处于操作状态的实际文件列表,文件列表 名柄保存在hNameMappings成员中。SHFILEOPSTRUCT结构还包含一个SHNAMEMAPPING结构的数组,此数组 保存由SHELL计算的每个处于操作状态的文件的新旧路径。
说明: 1.以下封装了4个函数_CopyFile,_DeleteFile,_MoveFile,_ReNameFile 2.每个函数都先拷贝了原来的路径,保证了路径是以2个/0/0结尾。(如果不以2个/0/0结尾,往往会出错) 3.对于文件或文件夹都适用 */
/**************************************************************************** 复制文件或者文件夹,需要把路径添加2个/0/0 ****************************************************************************/ static bool _CopyFile(const TCHAR *_pFrom,const TCHAR *_pTo,WORD flags=FOF_NOCONFIRMATION) { TCHAR pTo[MAX_PATH]={0}; _tcscpy(pTo,_pTo); TCHAR pFrom[MAX_PATH]={0}; _tcscpy(pFrom,_pFrom);
SHFILEOPSTRUCT FileOp={0}; FileOp.fFlags= flags; FileOp.pFrom = pFrom; FileOp.pTo = pTo; FileOp.wFunc = FO_COPY; return SHFileOperation(&FileOp) == 0; }
/**************************************************************************** 删除文件或者文件夹,需要把路径添加2个/0/0 ****************************************************************************/ static bool _DeleteFile(const TCHAR* _pFrom,WORD flags=FOF_ALLOWUNDO|FOF_NOCONFIRMATION) { TCHAR pFrom[MAX_PATH]={0}; _tcscpy(pFrom,_pFrom);
SHFILEOPSTRUCT FileOp={0}; FileOp.pFrom = pFrom; FileOp.pTo = NULL; //一定要是NULL FileOp.fFlags= flags; FileOp.wFunc = FO_DELETE; //删除操作 return SHFileOperation(&FileOp) == 0; }
/**************************************************************************** 移动文件或者文件夹,需要把路径添加2个/0/0 ****************************************************************************/ static bool _MoveFile(const TCHAR *_pFrom,const TCHAR *_pTo,WORD flags=FOF_NOCONFIRMATION) { TCHAR pTo[MAX_PATH]={0}; _tcscpy(pTo,_pTo); TCHAR pFrom[MAX_PATH]={0}; _tcscpy(pFrom,_pFrom);
SHFILEOPSTRUCT FileOp={0}; FileOp.fFlags= flags; FileOp.pFrom = pFrom; FileOp.pTo = pTo; FileOp.wFunc = FO_MOVE; return SHFileOperation(&FileOp) == 0; }
/**************************************************************************** 重命名文件或者文件夹 可以的话用 rename(buf,TempFilePath) (文件和文件夹都可以) because不需要把路径添加2个/0/0 ****************************************************************************/ static bool _ReNameFile(const TCHAR *_pFrom,const TCHAR *_pTo,WORD flags=FOF_NOCONFIRMATION) { TCHAR pTo[MAX_PATH]={0}; _tcscpy(pTo,_pTo); TCHAR pFrom[MAX_PATH]={0}; _tcscpy(pFrom,_pFrom);
SHFILEOPSTRUCT FileOp={0}; FileOp.fFlags = flags; FileOp.pFrom = pFrom; FileOp.pTo = pTo; FileOp.wFunc = FO_RENAME; return SHFileOperation(&FileOp) == 0; }
|
调用示例:
void CDemoDlg::OnButton1() { bool rel=_CopyFile(_T("c://t.bmp"),_T("C://pic_1.bmp") ); traceRel(rel,_T("_CopyFile")); }
void CDemoDlg::OnButton2() { bool rel=_CopyFile(_T("c://t.bmp"),_T("C://pic_1.bmp"),FOF_SILENT); traceRel(rel,_T("_CopyFile")); }
void CDemoDlg::OnButton3() { //复制文件夹并且重命名,需要FOF_MULTIDESTFILES //当覆盖时如果里面有的文件不会被删除 bool rel=_CopyFile(_T("c://a"),_T("C://d"),FOF_MULTIDESTFILES|FOF_NOCONFIRMATION); traceRel(rel,_T("_CopyFile")); }
void CDemoDlg::OnButton4() { //复制文件到指定目录下,不需要 FOF_MULTIDESTFILES,pTo目录要有效,否则虽能复制,但结果怪怪的 bool rel=_CopyFile(_T("c://a"),_T("C://d"),FOF_NOCONFIRMATION); traceRel(rel,_T("_CopyFile")); }
void CDemoDlg::OnButton5() { bool rel=_DeleteFile(_T("C://d"),FOF_ALLOWUNDO|FOF_NOCONFIRMATION/*|FOF_SILENT*/); traceRel(rel,_T("_DeleteFile")); }
void CDemoDlg::OnButton6() { bool rel=_MoveFile(_T("c://t.bmp"),_T("d://erw.bmp"),FOF_NOCONFIRMATION|FOF_SILENT); traceRel(rel,_T("_CopyFile")); }
void CDemoDlg::OnButton7() { bool rel=_ReNameFile(_T("c://t.bmp"),_T("c://v.bmp"),FOF_NOCONFIRMATION|FOF_SILENT); traceRel(rel,_T("_ReNameFile")); }
void CDemoDlg::OnButton8() { bool rel=_ReNameFile(_T("c://a"),_T("c://b"),FOF_NOCONFIRMATION|FOF_SILENT); traceRel(rel,_T("_ReNameFile")); } |
|