C++遞歸刪除一個目錄實例
更新時間:2014年10月14日 09:50:52 投稿:shichen2014
這篇文章主要介紹了C++遞歸刪除一個目錄的實現(xiàn)方法,涉及到目錄的操作及遞歸算法的應(yīng)用,需要的朋友可以參考下
本文實例講述了C++遞歸刪除一個目錄的實現(xiàn)方法。分享給大家供大家參考。具體方法如下:
CFindFile的使用框架如下:
復(fù)制代碼 代碼如下:
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();
}
遞歸刪除代碼如下:
復(fù)制代碼 代碼如下:
//循環(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++程序設(shè)計有所幫助。
您可能感興趣的文章:
- C++實現(xiàn)單張圖片讀取和保存
- c++讀取數(shù)據(jù)文件到數(shù)組的實例
- VC++實現(xiàn)文件與應(yīng)用程序關(guān)聯(lián)的方法(注冊表修改)
- C++實現(xiàn)修改函數(shù)代碼HOOK的封裝方法
- 利用C++如何覆蓋或刪除指定位置的文件內(nèi)容
- C++如何刪除map容器中指定值的元素詳解
- C++中關(guān)于set刪除的一些坑
- 淺談c++ vector和map的遍歷和刪除對象
- 詳解在C++中顯式默認(rèn)設(shè)置的函數(shù)和已刪除的函數(shù)的方法
- C++刪除指定文件夾下N天及之前日志文件的方法
- C++ vector刪除符合條件的元素示例分享
- C++操作文件進(jìn)行讀取、刪除、修改指定行
相關(guān)文章
c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯誤問題
Lambda表達(dá)式是現(xiàn)代C++的一個語法糖,挺好用的。但是如果使用不當(dāng),會導(dǎo)致內(nèi)存泄露或潛在的崩潰問題,這里總結(jié)下c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯誤問題,感興趣的朋友一起看看吧2023-02-02
C語言實現(xiàn)銷售管理系統(tǒng)設(shè)計
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)銷售管理系統(tǒng)設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
C++實現(xiàn)LeetCode(205.同構(gòu)字符串)
這篇文章主要介紹了C++實現(xiàn)LeetCode(205.同構(gòu)字符串),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++ 字符串string和整數(shù)int的互相轉(zhuǎn)化操作
這篇文章主要介紹了C++ 字符串string和整數(shù)int的互相轉(zhuǎn)化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12

