VS工程编译完之后会产生大量的临时文件,这个小程序就是清理VS工程残留文件的,能迅速清理VS的工程,如果有子文件夹递归进行清理,程序使用多线程效率很高。
/* * filename: main.cpp * author: lougd * created: 2014-12-26 10:25 * version: 1.0.0.1 * desc: clear vs project * version: * history: */ #include <Windows.h> #include <stdio.h> #include <set> #include <list> #include <string> #include <Shlwapi.h> #pragma comment(lib, "shlwapi.lib") using namespace std; size_t g_busy_count = 0; set<string> g_exts; set<string> g_dirs; list<string> g_dir_list; HANDLE g_dir_lock = CreateMutexA(NULL, FALSE, NULL); HANDLE g_work = CreateEventA(NULL, FALSE, FALSE, NULL); HANDLE g_exit = CreateEventA(NULL, TRUE, FALSE, NULL); VOID WINAPI Lock() { WaitForSingleObject(g_dir_lock, INFINITE); } VOID WINAPI UnLock() { ReleaseMutex(g_dir_lock); } VOID WINAPI DeleteDirWithFile(const char *dir) { list<string> files; list<string> dirs; WIN32_FIND_DATAA data; char tmp[512] = {0x00}; char path[512] = {0x00}; int mark = lstrlenA(dir); lstrcatA(tmp, dir); lstrcatA(path, dir); PathAppendA(path, "*"); HANDLE h = FindFirstFileA(path, &data); if (!h || INVALID_HANDLE_VALUE == h) { return; } do { if (0 == lstrcmpA(".", data.cFileName) || 0 == lstrcmpA("..", data.cFileName) || 0 == lstrlenA(data.cFileName)) { if (!FindNextFileA(h, &data)) { break; } continue; } PathAppendA(tmp, data.cFileName); if (PathIsDirectoryA(tmp)) { dirs.push_back(tmp); } else { files.push_back(tmp); } tmp[mark] = 0x00; } while (FindNextFileA(h, &data)); if (h && INVALID_HANDLE_VALUE != h) { FindClose(h); } list<string>::iterator itm; for (itm = dirs.begin() ; itm != dirs.end() ; itm++) { DeleteDirWithFile(itm->c_str()); } for (itm = files.begin() ; itm != files.end() ; itm++) { DeleteFileA(itm->c_str()); } RemoveDirectoryA(dir); } VOID WINAPI RecursionClearDir(const char *dir) { list<string> files; list<string> dirs; WIN32_FIND_DATAA data; char tmp[512] = {0x00}; char path[512] = {0x00}; int mark = lstrlenA(dir); lstrcatA(tmp, dir); lstrcatA(path, dir); PathAppendA(path, "*"); HANDLE h = FindFirstFileA(path, &data); if (!h || INVALID_HANDLE_VALUE == h) { return; } do { if (0 == lstrcmpA(".", data.cFileName) || 0 == lstrcmpA("..", data.cFileName) || 0 == lstrlenA(data.cFileName)) { if (!FindNextFileA(h, &data)) { break; } continue; } PathAppendA(tmp, data.cFileName); if (PathIsDirectoryA(tmp)) { if (g_dirs.end() != g_dirs.find(data.cFileName)) { dirs.push_back(tmp); } Lock(); g_dir_list.push_back(string(tmp)); UnLock(); SetEvent(g_work); } else { if (g_exts.end() != g_exts.find(PathFindExtensionA(tmp))) { files.push_back(tmp); } } tmp[mark] = 0x00; } while (FindNextFileA(h, &data)); list<string>::iterator itm; for (itm = dirs.begin() ; itm != dirs.end() ; itm++) { DeleteDirWithFile(itm->c_str()); } for (itm = files.begin() ; itm != files.end() ; itm++) { DeleteFileA(itm->c_str()); } if (h && INVALID_HANDLE_VALUE != h) { FindClose(h); } } DWORD WINAPI ProcessThread(LPVOID param) { HANDLE arry[] = {g_work, g_exit}; while(TRUE) { DWORD ret = WaitForMultipleObjects(sizeof(arry) / sizeof(HANDLE), arry, FALSE, INFINITE); if (WAIT_OBJECT_0 == ret) { string path; Lock(); g_busy_count++; UnLock(); while(TRUE) { Lock(); count = g_dir_list.size(); if (count <= 0) { UnLock(); break; } path = g_dir_list.front(); g_dir_list.pop_front(); UnLock(); RecursionClearDir(path.c_str()); } Lock(); g_busy_count--; if (0 == g_busy_count && 0 == g_dir_list.size()) { SetEvent(g_exit); } UnLock(); } if ((WAIT_OBJECT_0 + 1) == ret) { break; } } return 0; } VOID WINAPI GetFileType() { g_exts.insert(".ncb"); g_exts.insert(".suo"); g_exts.insert(".exp"); g_exts.insert(".pdb"); g_exts.insert(".ilk"); g_exts.insert(".user"); g_exts.insert(".aps"); g_exts.insert(".obj"); g_exts.insert(".idb"); g_exts.insert(".dep"); g_exts.insert(".sbr"); g_exts.insert(".tmp"); g_exts.insert(".tlh"); g_exts.insert(".tli"); g_exts.insert(".tlog"); g_exts.insert(".log"); g_exts.insert(".htm"); g_exts.insert(".clw"); g_exts.insert(".dsp"); g_exts.insert(".opt"); g_exts.insert(".sdf"); g_exts.insert(".filters"); g_exts.insert(".plg"); g_exts.insert(".positions"); g_exts.insert(".svn-base"); g_dirs.insert(".svn"); g_dirs.insert("Debug"); } int main(int argc, char *argv[]) { if (argc < 2) { printf("param error\n"); } if (0 == lstrcmpiA(argv[1], "/c")) { string path = argv[2]; SYSTEM_INFO system = {0}; GetSystemInfo(&system); int cpu = system.dwNumberOfProcessors; g_dir_list.push_back(path.c_str()); SetEvent(g_work); GetFileType(); int itm; HANDLE *ws = new HANDLE[cpu + 2]; for (itm = 0 ; itm < (cpu + 2) ; itm++) { ws[itm] = CreateThread(NULL, 0, ProcessThread, 0, 0, NULL); } WaitForMultipleObjects(cpu + 2, ws, TRUE, INFINITE); delete []ws; } return 0; }
时间: 2024-10-05 04:44:50