当前位置: 首页 > news >正文

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. }

转载于:https://www.cnblogs.com/dyx1024/archive/2008/08/02/2556822.html

相关文章:

  • 给GridView、Repeater、DataList的行增加编号、序号
  • 电脑高级应用精华
  • windows命令全集
  • NP642-825 号外
  • 谁在用苹果手机!?
  • Asp.net Mvc Codeplex Preview 5 第一篇 Helper的新特性
  • 在用c#开发的ActiveX中调用JavaScript方法
  • 中学信息中心解决方案
  • Cisco实物图片库
  • IBM联合盟友向微软发难 系统软件岂能一家独大
  • 有用网站收集
  • C语言实现正余弦函数图像的输出!(源码)
  • linux如何知道某个端口运行的是什么程序
  • 回收站无法清空
  • 一些IT开发工程师的悲哀
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • css的样式优先级
  • express.js的介绍及使用
  • gulp 教程
  • Java Agent 学习笔记
  • JavaScript 奇技淫巧
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • ViewService——一种保证客户端与服务端同步的方法
  • Vue组件定义
  • 程序员该如何有效的找工作?
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • ​flutter 代码混淆
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • # Maven错误Error executing Maven
  • #if和#ifdef区别
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (27)4.8 习题课
  • (day 12)JavaScript学习笔记(数组3)
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (汇总)os模块以及shutil模块对文件的操作
  • (强烈推荐)移动端音视频从零到上手(上)
  • (十五)使用Nexus创建Maven私服
  • (五)IO流之ByteArrayInput/OutputStream
  • .NET CORE Aws S3 使用
  • .net core Swagger 过滤部分Api
  • .net on S60 ---- Net60 1.1发布 支持VS2008以及新的特性
  • .NET单元测试
  • .NET建议使用的大小写命名原则
  • .NET使用存储过程实现对数据库的增删改查
  • .net网站发布-允许更新此预编译站点
  • [].slice.call()将类数组转化为真正的数组
  • [asp.net core]project.json(2)
  • [BZOJ3757] 苹果树