長期開發會產生大量 VC 專案,Debug/Release 目錄、.ncb/.opt/.plg 等中間檔案佔用大量磁碟。本文提供一套「遞歸掃描 + 精準刪除」的自動化清理方案,保留源碼與必要輸出,一鍵瘦身專案目錄。
| 類型 | 副檔名/目錄名 | 說明 |
|---|---|---|
| 瀏覽資訊 | .ncb | ClassWizard/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);
}
掃描目錄下是否存在 *.dsw(VC6)或 *.sln(VC7+),存在即視為專案根目錄,啟動清理邏輯。
CleanVC.exe,命令列參數傳入根目錄:CleanVC.exe D:\Projects。將此工具整合至專案管理腳本或定時任務,可持續維持開發環境乾淨高效,避免磁碟被無用中間檔案侵蝕。