VC 遍历指定目录下的文件

  1. //转自:http://www.vcgood.com/forum_posts.asp?TID=2261&PN=1


  2. //用于输出指定目录下的所有文件的文件名,包括子目录。
  3. 版本1:用string处理,方便,容易理解.
  4. #include <windows.h>
  5. #include <iostream>
  6. #include <string>
  7. using namespace std;
  8. bool IsRoot(string Path)
  9. {
  10. string Root;
  11. Root=Path.at(0)+"://";
  12. if(Root==Path)
  13.    return true;
  14. else
  15.    return false;
  16. }
  17. void FindInAll(string Path)
  18. {
  19. string szFind;
  20. szFind=Path;
  21. if(!IsRoot(szFind))
  22.    szFind+="//";
  23. szFind+="*.*";
  24. WIN32_FIND_DATA FindFileData;
  25. HANDLE hFind=FindFirstFile(szFind.c_str(),& FindFileData);
  26. if(hFind==INVALID_HANDLE_VALUE)
  27.    return ;
  28. do
  29. {
  30.    if(FindFileData.cFileName[0]=='.'//过滤本级目录和父目录
  31.     continue;
  32.    if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //如果找到的是目录,则进入此目录进行递归
  33.    {
  34.     string szFile;
  35.     if(IsRoot(Path))
  36.      szFile=Path+FindFileData.cFileName;
  37.     else
  38.      szFile=Path+"//"+FindFileData.cFileName;
  39.     FindInAll(szFile);
  40.    }
  41.    else //找到的是文件
  42.    {
  43.     string szFile;
  44.     if(IsRoot(Path))
  45.      szFile=Path+FindFileData.cFileName;
  46.     else
  47.      szFile=Path+"//"+FindFileData.cFileName;
  48.     cout<<szFile<<endl;
  49.     cout<<FindFileData.cFileName<<endl;
  50.    }
  51. }
  52. while(FindNextFile(hFind,& FindFileData));
  53. FindClose(hFind);
  54. }
  55. int main()
  56. {
  57. FindInAll("D://C++");
  58. return 0;
  59. }
  60. 版本2:编译器的通用性更强
  61. #include <windows.h>
  62. #include <iostream>
  63. using namespace std;
  64. BOOL IsRoot(LPCTSTR lpszPath)
  65. {
  66. TCHAR szRoot[4];
  67. wsprintf(szRoot,"%c://",lpszPath[0]);
  68. return (lstrcmp(szRoot,lpszPath)==0);
  69. }
  70. void FindInAll(::LPCTSTR lpszPath)
  71. {
  72. TCHAR szFind[MAX_PATH];
  73. lstrcpy(szFind,lpszPath); //windows API 用lstrcpy,不是strcpy
  74. if(!IsRoot(szFind))
  75.    lstrcat(szFind,"//");
  76. lstrcat(szFind,"*.*"); //找所有文件
  77. WIN32_FIND_DATA wfd;
  78. HANDLE hFind=FindFirstFile(szFind,& wfd);
  79. if(hFind==INVALID_HANDLE_VALUE) // 如果没有找到或查找失败
  80.    return;
  81. do
  82. {
  83.    if(wfd.cFileName[0]=='.')
  84.     continue//过滤这两个目录
  85.    if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  86.    {
  87.     TCHAR szFile[MAX_PATH];
  88.     if(IsRoot(lpszPath))
  89.      wsprintf(szFile,"%s%s",lpszPath,wfd.cFileName);
  90.     else
  91.      wsprintf(szFile,"%s//%s",lpszPath,wfd.cFileName);
  92.     FindInAll(szFile); //如果找到的是目录,则进入此目录进行递归
  93.    }
  94.    else
  95.    {
  96.     TCHAR szFile[MAX_PATH];
  97.     if(IsRoot(lpszPath))
  98.      wsprintf(szFile,"%s%s",lpszPath,wfd.cFileName);
  99.     else
  100.      wsprintf(szFile,"%s//%s",lpszPath,wfd.cFileName);
  101.     printf("%s/n",szFile); //对文件进行操作
  102.    }
  103. }
  104. while(FindNextFile(hFind,&wfd));
  105. FindClose(hFind); //关闭查找句柄
  106. }
  107. int main()
  108. {
  109. FindInAll("D://C++");
  110. return 0;
  111. }
posted @ 2008-08-02 22:14  Socrates  阅读(227)  评论(0编辑  收藏  举报