`
dyx1024
  • 浏览: 114797 次
  • 性别: Icon_minigender_1
  • 来自: 西安
文章分类
社区版块
存档分类
最新评论

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. usingnamespacestd;
  8. boolIsRoot(stringPath)
  9. {
  10. stringRoot;
  11. Root=Path.at(0)+"://";
  12. if(Root==Path)
  13. returntrue;
  14. else
  15. returnfalse;
  16. }
  17. voidFindInAll(stringPath)
  18. {
  19. stringszFind;
  20. szFind=Path;
  21. if(!IsRoot(szFind))
  22. szFind+="//";
  23. szFind+="*.*";
  24. WIN32_FIND_DATAFindFileData;
  25. HANDLEhFind=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. stringszFile;
  35. if(IsRoot(Path))
  36. szFile=Path+FindFileData.cFileName;
  37. else
  38. szFile=Path+"//"+FindFileData.cFileName;
  39. FindInAll(szFile);
  40. }
  41. else//找到的是文件
  42. {
  43. stringszFile;
  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. intmain()
  56. {
  57. FindInAll("D://C++");
  58. return0;
  59. }
  60. 版本2:编译器的通用性更强
  61. #include<windows.h>
  62. #include<iostream>
  63. usingnamespacestd;
  64. BOOLIsRoot(LPCTSTRlpszPath)
  65. {
  66. TCHARszRoot[4];
  67. wsprintf(szRoot,"%c://",lpszPath[0]);
  68. return(lstrcmp(szRoot,lpszPath)==0);
  69. }
  70. voidFindInAll(::LPCTSTRlpszPath)
  71. {
  72. TCHARszFind[MAX_PATH];
  73. lstrcpy(szFind,lpszPath);//windowsAPI用lstrcpy,不是strcpy
  74. if(!IsRoot(szFind))
  75. lstrcat(szFind,"//");
  76. lstrcat(szFind,"*.*");//找所有文件
  77. WIN32_FIND_DATAwfd;
  78. HANDLEhFind=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. TCHARszFile[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. TCHARszFile[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. intmain()
  108. {
  109. FindInAll("D://C++");
  110. return0;
  111. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics