http://www.cnblogs.com/codeape/archive/2013/08/10/3250395.html
-
#include <Windows.h>
-
#include <stdint.h>
-
#include <cstdio>
-
#include <cstring>
-
#include <string>
-
#include <queue>
-
-
-
typedef int8_t (__stdcall *P_WALKDIR_CALLBACK)(const char *In_pcFilePath);
-
-
int8_t WalkDir(const char *In_pcRootDir, P_WALKDIR_CALLBACK In_pfunCallBack)
-
{
-
int8_t i8RetVal = 0;
-
std::string strLocalRoot;
-
std::queue<std::string> qDirectory;
-
-
if (In_pcRootDir == NULL || In_pfunCallBack == NULL)
-
{
-
i8RetVal = -1;
-
goto fun_ret;
-
}
-
-
strLocalRoot = In_pcRootDir;
-
if (strLocalRoot.empty())
-
{
-
i8RetVal = -2;
-
goto fun_ret;
-
}
-
-
char cRootBack = strLocalRoot.back();
-
if (cRootBack != '\\' && cRootBack != '/')
-
{
-
strLocalRoot += '\\';
-
}
-
qDirectory.push(strLocalRoot);
-
-
do
-
{
-
std::string strDirForWalk = qDirectory.front();
-
WIN32_FIND_DATAA Win32FindData = {0};
-
HANDLE hFindHandle = NULL;
-
-
qDirectory.pop();
-
hFindHandle = FindFirstFileA((strDirForWalk + "*").c_str(), &Win32FindData);
-
if (hFindHandle == INVALID_HANDLE_VALUE)
-
{
-
continue;
-
}
-
if (strcmp(Win32FindData.cFileName, ".") != 0 && strcmp(Win32FindData.cFileName, "..") != 0)
-
{
-
if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
-
{
-
qDirectory.push(strDirForWalk + Win32FindData.cFileName + "\\");
-
}
-
else
-
{
-
In_pfunCallBack((strDirForWalk + Win32FindData.cFileName).c_str());
-
}
-
}
-
-
while (FindNextFileA(hFindHandle, &Win32FindData))
-
{
-
if (strcmp(Win32FindData.cFileName, ".") != 0 && strcmp(Win32FindData.cFileName, "..") != 0)
-
{
-
if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
-
{
-
qDirectory.push(strDirForWalk + Win32FindData.cFileName + "\\");
-
}
-
else
-
{
-
In_pfunCallBack((strDirForWalk + Win32FindData.cFileName).c_str());
-
}
-
}
-
}
-
-
if (hFindHandle != NULL)
-
{
-
FindClose(hFindHandle);
-
}
-
} while (!qDirectory.empty());
-
-
fun_ret:
-
return i8RetVal;
-
}
-
-
int8_t __stdcall WalkDirCallBack(const char *In_pcFilePath)
-
{
-
if (In_pcFilePath != NULL)
-
{
-
printf("%s\n", In_pcFilePath);
-
}
-
return 0;
-
}
-
-
void main(int argc, char **argv)
-
{
-
WalkDir(argv[1], WalkDirCallBack);
-
return;
-
}
阅读(886) | 评论(0) | 转发(0) |