#include <Windows.h>
#include <WinDef.h >
#include <iostream>
using namespace std;
DWORD dwTotalFileNum = 0;
DWORD WINAPI ThreadProc(LPVOID lpParameter);
void Fun(WCHAR* pszDirName);
CRITICAL_SECTION m_cs;
void TraversalDir(WCHAR *szDir)
{
WCHAR szFilePath[MAX_PATH] = L" ";
WIN32_FIND_DATA FindFileData = {0};
HANDLE hListFile;
WCHAR szFullPath[MAX_PATH] = L" ";
//构造代表子目录和文件夹路径的字符串,使用通配符“*”
wcscpy_s(szFilePath, szDir);
wcscat_s(szFilePath, L"//*");
hListFile = FindFirstFile(szFilePath, &FindFileData);
if(hListFile == INVALID_HANDLE_VALUE)
{
printf("Invalid File Handle. GetLastError reports %d/n", GetLastError ());
return ;
}
else
{
do
{//过滤“.”和“..”,不需要遍历
if(lstrcmp(FindFileData.cFileName,TEXT(".")) == 0 || lstrcmp(FindFileData.cFileName,TEXT("..")) == 0)
{
continue;
}
/* EnterCriticalSection(&m_cs);*/
wsprintf(szFullPath,L"%s//%s",szDir, FindFileData.cFileName);
/*
LeaveCriticalSection(&m_cs);
*/
if(!(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
{
_putws(szFullPath);
printf("/n");
/*
EnterCriticalSection(&m_cs);
dwTotalFileNum++;
LeaveCriticalSection(&m_cs);*/
}
else
{
//TraversalDir(szFullPath);
Fun(szFullPath);
}
//如果是目录,则递归调用,列举下级目录
}while(FindNextFile(hListFile, &FindFileData));
}
FindClose(hListFile);
}
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
TraversalDir((WCHAR*) lpParameter);
free(lpParameter);
return 0;
}
void Fun(WCHAR* pszDirName)
{
int len = (wcslen(pszDirName) + 10) * sizeof(WCHAR);
WCHAR *tp = (WCHAR*)malloc(len);
memset(tp, 0, len);
wcscpy(tp, pszDirName);
HANDLE hProducerThread = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE )ThreadProc, tp, 0, NULL);
}
int main()
{
InitializeCriticalSection(&m_cs);
DWORD dwStartCurrTime, dwEndCurrTime;
dwStartCurrTime = GetCurrentTime ();
TraversalDir(L"C:");
//printf("/n%d/n", dwTotalFileNum);
dwEndCurrTime = GetCurrentTime();
printf("The cost time is :%d/n", dwEndCurrTime - dwStartCurrTime);
DeleteCriticalSection(&m_cs);
return 0;
}