C++遞歸刪除一個(gè)目錄實(shí)例
更新時(shí)間:2014年10月14日 09:50:52 投稿:shichen2014
這篇文章主要介紹了C++遞歸刪除一個(gè)目錄的實(shí)現(xiàn)方法,涉及到目錄的操作及遞歸算法的應(yīng)用,需要的朋友可以參考下
本文實(shí)例講述了C++遞歸刪除一個(gè)目錄的實(shí)現(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)刪除一個(gè)目錄
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;
}
希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- C++實(shí)現(xiàn)單張圖片讀取和保存
- c++讀取數(shù)據(jù)文件到數(shù)組的實(shí)例
- VC++實(shí)現(xiàn)文件與應(yīng)用程序關(guān)聯(lián)的方法(注冊(cè)表修改)
- C++實(shí)現(xiàn)修改函數(shù)代碼HOOK的封裝方法
- 利用C++如何覆蓋或刪除指定位置的文件內(nèi)容
- C++如何刪除map容器中指定值的元素詳解
- C++中關(guān)于set刪除的一些坑
- 淺談c++ vector和map的遍歷和刪除對(duì)象
- 詳解在C++中顯式默認(rèn)設(shè)置的函數(shù)和已刪除的函數(shù)的方法
- C++刪除指定文件夾下N天及之前日志文件的方法
- C++ vector刪除符合條件的元素示例分享
- C++操作文件進(jìn)行讀取、刪除、修改指定行
相關(guān)文章
淺談C語言的字節(jié)對(duì)齊 #pragma pack(n)2
下面小編就為大家?guī)硪黄獪\談C語言的字節(jié)對(duì)齊 #pragma pack(n)2。小編覺得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01C++中可以接受任意多個(gè)參數(shù)的函數(shù)定義方法(詳解)
下面小編就為大家?guī)硪黄狢++中可以接受任意多個(gè)參數(shù)的函數(shù)定義方法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10C++ VTK實(shí)例之高斯隨機(jī)數(shù)的生成
這篇文章主要介紹了VTK的一個(gè)實(shí)例之高斯隨機(jī)數(shù)的生成,本文演示了從一個(gè)平均數(shù)是0.0和標(biāo)準(zhǔn)偏差是2.2的高斯分布中隨機(jī)生成3個(gè)隨機(jī)數(shù)。感興趣的同學(xué)可以學(xué)習(xí)一下2021-11-11VisualStudio2019構(gòu)建C/C++靜態(tài)庫和動(dòng)態(tài)庫dll的問題 附源碼
這篇文章主要介紹了VisualStudio2019構(gòu)建C/C++靜態(tài)庫和動(dòng)態(tài)庫(dll)(文末附源碼),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03