// 广度遍历目录
BOOL TraverseDirBF(const string& strDir)
{
deque<string> dequePath;
string strTemp = strDir;
int iFileCount = 0;
int iDirCount = 0;
ofstream ofs("out.txt");
while(TRUE)
{
if (!PathFileExists(strTemp.c_str()))
{
return FALSE;
}
string strDirFind = strTemp + string("\\*");
WIN32_FIND_DATA ffd = {0};
HANDLE hFind = FindFirstFile(strDirFind.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
//return FALSE;
if (dequePath.empty())
{
break;
}
else
{
strTemp = dequePath.front();
dequePath.pop_front();
continue;
}
}
// List all the files in the directory with some info about them.
do
{
if ((0 == strcmp(ffd.cFileName, ".")) || (0 == strcmp(ffd.cFileName, "..")))
{
continue;
}
string strFilePathName = strTemp + "\\" + string(ffd.cFileName);
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//_tprintf(TEXT(" %s <DIR>\n"), strFilePathName.c_str());
string str111 = strFilePathName + "\n";
ofs.write(str111.c_str(), str111.size());
dequePath.push_back(strFilePathName);
++iDirCount;
}
else
{
//_tprintf(TEXT(" %s <FILE>\n"), strFilePathName.c_str());
string str111 = strFilePathName + "\n";
ofs.write(str111.c_str(), str111.size());
++iFileCount;
}
}
while (FindNextFile(hFind, &ffd) != 0);
DWORD dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES)
{
FindClose(hFind);
return FALSE;
}
FindClose(hFind);
if (dequePath.empty())
{
break;
}
else
{
strTemp = dequePath.front();
dequePath.pop_front();
}
}
ofs.close();
return TRUE;
}
// 深度优先遍历
BOOL TraverseDirDF(const string& strDir, int& iFileCount, int& iDirCount)
{
if (!PathFileExists(strDir.c_str()))
{
return FALSE;
}
string strDirFind = strDir + string("\\*");
WIN32_FIND_DATA ffd = {0};
HANDLE hFind = FindFirstFile(strDirFind.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
return FALSE;
}
do
{
if ((0 == strcmp(ffd.cFileName, ".")) || (0 == strcmp(ffd.cFileName, "..")))
{
continue;
}
string strFilePathName = strDir + "\\" + string(ffd.cFileName);
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
++iDirCount;
TraverseDirDF(strFilePathName, iFileCount, iDirCount);
}
else
{
++iFileCount;
}
}
while (FindNextFile(hFind, &ffd) != 0);
DWORD dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES)
{
FindClose(hFind);
return FALSE;
}
FindClose(hFind);
return TRUE;
}