Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1542681
  • 博文数量: 596
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 173
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-06 15:50
个人简介

在线笔记

文章分类

全部博文(596)

文章存档

2016年(1)

2015年(104)

2014年(228)

2013年(226)

2012年(26)

2011年(11)

分类: Windows平台

2014-10-21 12:10:57

http://www.cnblogs.com/codeape/archive/2013/08/10/3250395.html
  1. #include <Windows.h>
  2. #include <stdint.h>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <string>
  6. #include <queue>


  7. typedef int8_t (__stdcall *P_WALKDIR_CALLBACK)(const char *In_pcFilePath);

  8. int8_t WalkDir(const char *In_pcRootDir, P_WALKDIR_CALLBACK In_pfunCallBack)
  9. {
  10.     int8_t i8RetVal = 0;
  11.     std::string strLocalRoot;
  12.     std::queue<std::string> qDirectory;

  13.     if (In_pcRootDir == NULL || In_pfunCallBack == NULL)
  14.     {
  15.         i8RetVal = -1;
  16.         goto fun_ret;
  17.     }

  18.     strLocalRoot = In_pcRootDir;
  19.     if (strLocalRoot.empty())
  20.     {
  21.         i8RetVal = -2;
  22.         goto fun_ret;
  23.     }

  24.     char cRootBack = strLocalRoot.back();
  25.     if (cRootBack != '\\' && cRootBack != '/')
  26.     {
  27.         strLocalRoot += '\\';
  28.     }
  29.     qDirectory.push(strLocalRoot);

  30.     do
  31.     {
  32.         std::string strDirForWalk = qDirectory.front();
  33.         WIN32_FIND_DATAA Win32FindData = {0};
  34.         HANDLE hFindHandle = NULL;

  35.         qDirectory.pop();
  36.         hFindHandle = FindFirstFileA((strDirForWalk + "*").c_str(), &Win32FindData);
  37.         if (hFindHandle == INVALID_HANDLE_VALUE)
  38.         {
  39.             continue;
  40.         }
  41.         if (strcmp(Win32FindData.cFileName, ".") != 0 && strcmp(Win32FindData.cFileName, "..") != 0)
  42.         {
  43.             if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  44.             {
  45.                 qDirectory.push(strDirForWalk + Win32FindData.cFileName + "\\");
  46.             }
  47.             else
  48.             {
  49.                 In_pfunCallBack((strDirForWalk + Win32FindData.cFileName).c_str());
  50.             }
  51.         }

  52.         while (FindNextFileA(hFindHandle, &Win32FindData))
  53.         {
  54.             if (strcmp(Win32FindData.cFileName, ".") != 0 && strcmp(Win32FindData.cFileName, "..") != 0)
  55.             {
  56.                 if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  57.                 {
  58.                     qDirectory.push(strDirForWalk + Win32FindData.cFileName + "\\");
  59.                 }
  60.                 else
  61.                 {
  62.                     In_pfunCallBack((strDirForWalk + Win32FindData.cFileName).c_str());
  63.                 }
  64.             }
  65.         }

  66.         if (hFindHandle != NULL)
  67.         {
  68.             FindClose(hFindHandle);
  69.         }
  70.     } while (!qDirectory.empty());

  71. fun_ret:
  72.     return i8RetVal;
  73. }

  74. int8_t __stdcall WalkDirCallBack(const char *In_pcFilePath)
  75. {
  76.     if (In_pcFilePath != NULL)
  77.     {
  78.         printf("%s\n", In_pcFilePath);
  79.     }
  80.     return 0;
  81. }

  82. void main(int argc, char **argv)
  83. {
  84.     WalkDir(argv[1], WalkDirCallBack);
  85.     return;
  86. }

阅读(886) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~