国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++遞歸刪除一個目錄實例

2020-01-26 15:14:18
字體:
來源:轉載
供稿:網友

本文實例講述了C++遞歸刪除一個目錄的實現方法。分享給大家供大家參考。具體方法如下:

CFindFile的使用框架如下:

復制代碼 代碼如下:
void Recurse(LPCTSTR pstr) 

   CFileFind finder; 
 
   // build a string with wildcards 
   CString strWildcard(pstr); 
   strWildcard += _T("http://*.*"); 
 
   // 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(); 
}

遞歸刪除代碼如下:

復制代碼 代碼如下:
//循環刪除一個目錄 
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++程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安徽省| 七台河市| 博湖县| 农安县| 慈溪市| 海宁市| 日土县| 霍邱县| 玛纳斯县| 岗巴县| 赞皇县| 柳河县| 丽江市| 石渠县| 景洪市| 彭泽县| 平度市| 肥城市| 都安| 会东县| 黄浦区| 吴桥县| 香格里拉县| 桂东县| 汤原县| 通海县| 正蓝旗| 昭苏县| 大渡口区| 华亭县| 武强县| 乐至县| 贡觉县| 文成县| 马尔康县| 静海县| 马龙县| 崇信县| 胶州市| 湖南省| 十堰市|