C++ 使用win32API遍历文件夹

来源:互联网 发布:多久修一次眉毛知乎 编辑:程序博客网 时间:2024/06/07 10:58
#include <windows.h>
02#include <stdio.h>
03#include <string.h>
04#define LEN 1024
05// 深度优先递归遍历目录中所有的文件
06BOOL  DirectoryList(LPCSTR Path)
07{
08 WIN32_FIND_DATA FindData;
09 HANDLEhError;
10 intFileCount = 0;
11 charFilePathName[LEN];
12 // 构造路径
13 charFullPathName[LEN];
14 strcpy(FilePathName, Path);
15 strcat(FilePathName,"\\*.*");
16 hError = FindFirstFile(FilePathName, &FindData);
17 if(hError == INVALID_HANDLE_VALUE)
18 {
19  printf("搜索失败!");
20  return0;
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);
33FileCount++;
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 return0;
47}
48  
49void main()
50{
51 DirectoryList("D:eclipse-J2EE");
52}
typedef struct _FILE_INFO {
  TCHAR szFileTitle[128]; //文件的标题名
  DWORD dwFileAttributes; //文件的属性
  FILETIME ftCreationTime; //文件的创建时间
  FILETIME ftLastAccessTime; //文件的最后访问时间
  FILETIME ftLastWriteTime; //文件的最后修改时间
  DWORD nFileSizeHigh; //文件大小的高位双字
  DWORD nFileSizeLow; //文件大小的低位双字
  DWORD dwReserved0; //保留,为0
  DWORD dwReserved1; //保留,为0
  } FILE_INFO, * PFILE_INFO; 

http://www.oschina.net/code/snippet_119226_6095