C++遍历文件夹下的文件

#include <windows.h>
02 #include <stdio.h>
03 #include <string.h>
04 #define LEN 1024
05 // 深度优先递归遍历目录中所有的文件
06 BOOL  DirectoryList(LPCSTR Path)
07 {
08  WIN32_FIND_DATA FindData;
09  HANDLE hError;
10  int FileCount = 0;
11  char FilePathName[LEN];
12  // 构造路径
13  char FullPathName[LEN];
14  strcpy(FilePathName, Path);
15  strcat(FilePathName, "\\*.*");
16  hError = FindFirstFile(FilePathName, &FindData);
17  if (hError == INVALID_HANDLE_VALUE)
18  {
19   printf("搜索失败!");
20   return 0;
21  }
22  while(::FindNextFile(hError, &FindData))
23  {
24   // 过虑.和..
25   if (strcmp(FindData.cFileName, ".") == 0
26    || strcmp(FindData.cFileName, "..") == 0 )
27   {
28    continue;
29  }
30    
31   // 构造完整路径
32  wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName);
33 FileCount++;
34  // 输出本级的文件
35  printf("\n%d  %s  ", FileCount, FullPathName);
36  
37   if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
38   {
39    printf("<Dir>");
40    DirectoryList(FullPathName);
41   }
42  
43  
44  
45  }
46  return 0;
47 }
48  
49 void main()
50 {
51  DirectoryList("D:eclipse-J2EE");
52 }

转载于:https://my.oschina.net/abcMx/blog/103212

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值