-
2009-06-10
删除文件夹中的文件 - [C++再学习]
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://xingzhesun.blogbus.com/logs/40815924.html
需要调用三个函数
//删除字符串最后的斜杠
1. void TrimSlash(CString& strTxt);//参数为被删除文件的路径
//删除文件夹中的文件和文件夹
2.void DeleteFilesAndFolders(LPCTSTR lpFiles);//参数为被删除文件的路径
应用举例:
//////////////////////////////////////
CString strPath(_T("D:\\sunhm@seepole.net\\"));
TrimSlash(strPath);
//
strPath += _T("\\");
//
DeleteFilesAndFolders(strPath);//////////////////////////////////////////////
void TrimSlash(CString& strTxt)
{
ATLASSERT(!strTxt.IsEmpty());int nFind = strTxt.ReverseFind('\\');
int nLen = strTxt.GetLength();
if (-1 != nFind && nFind == nLen - 1) //slash
{
strTxt.Delete(nFind);//
TrimSlash(strTxt);
}else{
return;
}}
///////////////////////////////////////////////////////////////////
void CLookFileDlg::DeleteFilesAndFolders( LPCTSTR lpFiles )
{
WIN32_FIND_DATA FileData; //
TCHAR szFileName[MAX_PATH] = {0}; //
CString strPath(lpFiles); //path//Get file attributes
DWORD dwAttri = GetFileAttributes(lpFiles);//directory
if (dwAttri == FILE_ATTRIBUTE_DIRECTORY)
{
//get child item
HANDLE hFile = FindFirstFile( strPath + _T("*.*"), &FileData);
CString strFile;
BOOL bFind = TRUE;
while (bFind)
{if(FILE_ATTRIBUTE_DIRECTORY == FileData.dwFileAttributes)
{
//all files is deleted
//delete folder
BOOL bRemove;
if (FileData.cFileName[0] != '.')//is not dot
{
DeleteFilesAndFolders(strPath + FileData.cFileName + _T("\\"));
bRemove = RemoveDirectory(strPath+ FileData.cFileName);
}
}else
{ZeroMemory(szFileName,sizeof(TCHAR)* MAX_PATH);
wcscpy(szFileName, FileData.cFileName);strFile.Empty();
strFile = strPath + szFileName;//delete file
BOOL bDel = DeleteFile(strFile);
}//find next item
ZeroMemory(&FileData, sizeof(WIN32_FIND_DATA));
bFind = FindNextFile(hFile, &FileData);
}
FindClose (hFile);
}
}随机文章:
集合 set 的实现 2007-02-07向量 vector的实现 2007-02-07template stact and interator 2007-02-02类中重载new&delete 2007-01-16struct and complier 2006-12-30
收藏到:Del.icio.us







