一个多线程清算vs工程的小工具

一个多线程清理vs工程的小工具
  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;
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 RecursionClearDir(const char *dir)
{
list<string> files;
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))
{
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 = 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++;
size_t count = g_dir_list.size();
if (count > 0)
{
path = g_dir_list.front();
g_dir_list.pop_front();
count = g_dir_list.size();
UnLock();
RecursionClearDir(path.c_str());
}
else
{
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");
}

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;
}

------解决思路----------------------
感谢分享
一个多线程清算vs工程的小工具
------解决思路----------------------
一个多线程清算vs工程的小工具
------解决思路----------------------
请教一下楼主花多长时间写出来的?
------解决思路----------------------
一个多线程清算vs工程的小工具