C++遞歸刪除一個目錄實例
更新時間:2014年10月14日 09:50:52 投稿:shichen2014
這篇文章主要介紹了C++遞歸刪除一個目錄的實現(xiàn)方法,涉及到目錄的操作及遞歸算法的應用,需要的朋友可以參考下
本文實例講述了C++遞歸刪除一個目錄的實現(xiàn)方法。分享給大家供大家參考。具體方法如下:
CFindFile的使用框架如下:
復制代碼 代碼如下:
void Recurse(LPCTSTR pstr)
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
// if it's a directory, recursively search it
if (finder.IsDirectory())
{
CString str = finder.GetFilePath();
TRACE(_T("%s\n"), (LPCTSTR)str);
Recurse(str);
}
}
finder.Close();
}
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
// if it's a directory, recursively search it
if (finder.IsDirectory())
{
CString str = finder.GetFilePath();
TRACE(_T("%s\n"), (LPCTSTR)str);
Recurse(str);
}
}
finder.Close();
}
遞歸刪除代碼如下:
復制代碼 代碼如下:
//循環(huán)刪除一個目錄
void RecursiveDelete(CString strDir)
{
CFileFind ff;
CString strPath;
strPath = strDir;
if (strPath.Right(1) != '\\')
{
strPath += '\\';
}
strPath += "*.*";
BOOL bWorking = ff.FindFile(strPath);
while (bWorking)
{
bWorking = ff.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (ff.IsDots())
continue;
// if it's a directory, recursively search it
if (ff.IsDirectory())
{
//遞歸目錄
CString str = ff.GetFilePath();
TRACE(_T("%s\n"), (LPCTSTR)str);
RecursiveDelete(str);
//刪除目錄
::SetFileAttributesA(str, FILE_ATTRIBUTE_NORMAL);
::RemoveDirectory(str);
}
else
{
//刪除文件
CString str = ff.GetFilePath();
TRACE(_T("%s\n"), (LPCTSTR)str);
::SetFileAttributes(str, FILE_ATTRIBUTE_NORMAL);
::DeleteFile(str);
}
}
ff.Close();
}
int main(int argc, char *argv[])
{
RecursiveDelete("C:\\20_128\\");
return 0;
}
void RecursiveDelete(CString strDir)
{
CFileFind ff;
CString strPath;
strPath = strDir;
if (strPath.Right(1) != '\\')
{
strPath += '\\';
}
strPath += "*.*";
BOOL bWorking = ff.FindFile(strPath);
while (bWorking)
{
bWorking = ff.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (ff.IsDots())
continue;
// if it's a directory, recursively search it
if (ff.IsDirectory())
{
//遞歸目錄
CString str = ff.GetFilePath();
TRACE(_T("%s\n"), (LPCTSTR)str);
RecursiveDelete(str);
//刪除目錄
::SetFileAttributesA(str, FILE_ATTRIBUTE_NORMAL);
::RemoveDirectory(str);
}
else
{
//刪除文件
CString str = ff.GetFilePath();
TRACE(_T("%s\n"), (LPCTSTR)str);
::SetFileAttributes(str, FILE_ATTRIBUTE_NORMAL);
::DeleteFile(str);
}
}
ff.Close();
}
int main(int argc, char *argv[])
{
RecursiveDelete("C:\\20_128\\");
return 0;
}
希望本文所述對大家的C++程序設計有所幫助。
您可能感興趣的文章:
- C++實現(xiàn)單張圖片讀取和保存
- c++讀取數(shù)據(jù)文件到數(shù)組的實例
- VC++實現(xiàn)文件與應用程序關聯(lián)的方法(注冊表修改)
- C++實現(xiàn)修改函數(shù)代碼HOOK的封裝方法
- 利用C++如何覆蓋或刪除指定位置的文件內容
- C++如何刪除map容器中指定值的元素詳解
- C++中關于set刪除的一些坑
- 淺談c++ vector和map的遍歷和刪除對象
- 詳解在C++中顯式默認設置的函數(shù)和已刪除的函數(shù)的方法
- C++刪除指定文件夾下N天及之前日志文件的方法
- C++ vector刪除符合條件的元素示例分享
- C++操作文件進行讀取、刪除、修改指定行
相關文章
淺談C語言的字節(jié)對齊 #pragma pack(n)2
下面小編就為大家?guī)硪黄獪\談C語言的字節(jié)對齊 #pragma pack(n)2。小編覺得挺不錯的現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01C++中可以接受任意多個參數(shù)的函數(shù)定義方法(詳解)
下面小編就為大家?guī)硪黄狢++中可以接受任意多個參數(shù)的函數(shù)定義方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10VisualStudio2019構建C/C++靜態(tài)庫和動態(tài)庫dll的問題 附源碼
這篇文章主要介紹了VisualStudio2019構建C/C++靜態(tài)庫和動態(tài)庫(dll)(文末附源碼),本文通過實例圖文相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03