首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> VC 教學

VC 專案瘦身實戰:遞歸清理無用中間檔案釋放磁碟空間

     編輯:VC 教學

長期開發會產生大量 VC 專案,Debug/Release 目錄、.ncb/.opt/.plg 等中間檔案佔用大量磁碟。本文提供一套「遞歸掃描 + 精準刪除」的自動化清理方案,保留源碼與必要輸出,一鍵瘦身專案目錄。

可安全刪除的檔案/目錄清單

類型副檔名/目錄名說明
瀏覽資訊.ncbClassWizard/IntelliSense 資料庫,重新開啟專案自動重建
工作區選項.opt視窗佈局等個人化設定,不影響編譯
建構日誌.plg編譯連結輸出記錄,純參考用
中間產物Debug/, Release/.obj/.pch/.ilk/.pdb 等,重新建構自動產生

保留策略:若需保留執行檔供測試,可僅保留 Debug/*.exe 或 Release/*.exe。

核心演算法:遞歸目錄遍歷

void CleanProject(const CString& rootPath) {
    CFileFind finder;
    BOOL bFound = finder.FindFile(rootPath + _T("\\*.*"));
    while (bFound) {
        bFound = finder.FindNextFile();
        if (finder.IsDots()) continue;

        if (finder.IsDirectory()) {
            // 遞歸進入子目錄
            CleanProject(finder.GetFilePath());
            // 空目錄可選擇性移除
        } else {
            CString ext = finder.GetFileName();
            ext.MakeLower();
            // 比對副檔名
            if (ext == _T(".ncb") || ext == _T(".opt") || ext == _T(".plg")) {
                DeleteFile(finder.GetFilePath());
            }
        }
    }
    finder.Close();

    // 刪除 Debug/Release 目錄(需在遞歸返回後處理)
    CString debugDir = rootPath + _T("\\Debug");
    CString releaseDir = rootPath + _T("\\Release");
    if (PathFileExists(debugDir))  RemoveDirectoryRecursive(debugDir);
    if (PathFileExists(releaseDir)) RemoveDirectoryRecursive(releaseDir);
}

判斷 VC 專案根目錄

掃描目錄下是否存在 *.dsw(VC6)或 *.sln(VC7+),存在即視為專案根目錄,啟動清理邏輯。

編譯與使用

  • 環境:Windows 98 / VC++ 6.0(原文環境),現代系統同樣適用。
  • 編譯為獨立 CleanVC.exe,命令列參數傳入根目錄:CleanVC.exe D:\Projects。
  • 執行前建議備份重要專案,確認保留策略符合需求。

將此工具整合至專案管理腳本或定時任務,可持續維持開發環境乾淨高效,避免磁碟被無用中間檔案侵蝕。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved