Windows和Linux下枚举文件夹下文件的方法

原文地址::http://blog.csdn.net/wutaozhao/article/details/6044067


相关文章

1、浅析linux中目录枚举的具体实现 ----http://blog.chinaunix.net/uid-20564848-id-73754.html

2、Linux下DIR,dirent,stat等结构体详解----http://wenku.baidu.com/link?url=_GWxd5QG7rYw3Muste5xTKYLZkXU1wWU6kd2xTU_nMdtHzTGk2vF56eNg71g9XOlWdvh4Izgq97hpVMy9tUmgM3vCgYyljbP0GuR4_a9Qmq


Windows下主要通过FindFirstFile和FindNextFile这两个API实现,每枚举到一个文件,都要判断WIN32_FIND_DATA的dwFileAttributes属性,看一下是目录还是文件,如果是目录,可以递归枚举

 

[cpp]  view plain  copy
  1. DWORD IterFiles(string srcPath, string destPath)  
  2. {  
  3.     DWORD dwStatus = 0;  
  4.   
  5.     WIN32_FIND_DATA findFileData;  
  6.     string filePath = srcPath, srcNewPath = srcPath, destNewPath = destPath;  
  7.     filePath += "\\*.*";  
  8.     HANDLE hFind = FindFirstFile(filePath.c_str(), &findFileData);  
  9.     if (hFind != INVALID_HANDLE_VALUE)  
  10.     {  
  11.         do   
  12.         {  
  13.             srcNewPath = srcPath + "\\" + findFileData.cFileName;  
  14.             destNewPath = destPath + "\\" + findFileData.cFileName;  
  15.             if (strcmp(findFileData.cFileName, ".") == 0 || strcmp(findFileData.cFileName, "..") == 0)  
  16.             {  
  17.                 continue;  
  18.             }  
  19.             else if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  
  20.             {  
  21.                 dwStatus = IterFiles(srcNewPath, destNewPath);  
  22.             }  
  23.             else  
  24.             {/* do something */  
  25.             }  
  26.             if (dwStatus != 0)  
  27.             {  
  28.                 break;  
  29.             }  
  30.         } while (FindNextFile(hFind, &findFileData));  
  31.     }  
  32.   
  33.     return dwStatus;  
  34. }  

 

 

Linux

 

[cpp]  view plain  copy
  1. bool CLinDriverRW::IsDirectory(char *pszName)  
  2. {  
  3.     struct stat S_stat;  
  4.   
  5.     //取得文件状态  
  6.     if (lstat(pszName, &S_stat) < 0)  
  7.     {  
  8.         return false;  
  9.     }  
  10.   
  11.     if (S_ISDIR(S_stat.st_mode))  
  12.     {  
  13.         return true;  
  14.     }  
  15.     else  
  16.     {  
  17.         return false;  
  18.     }  
  19. }  


 

[cpp]  view plain  copy
  1. DWORD IterFiles(string srcPath, string destPath)  
  2. {  
  3.     int iRet = 0;  
  4.     DIR *pDir;  
  5.     string destNewPath;  
  6.     struct dirent *pDirent;  
  7.     pDir = opendir(srcPath.c_str());  
  8.     if (pDir == NULL)  
  9.     {  
  10.         return -1;  
  11.     }  
  12.     while ((pDirent = readdir(pDir)) != NULL)  
  13.     {  
  14.         // if ((pDirent->d_name == '.') || pDirent->d_name[0] == '..')  
  15.         if (strcmp(pDirent->d_name, ".") == 0 || strcmp(pDirent->d_name, "..") == 0)  
  16.         {  
  17.             continue;  
  18.         }  
  19.         char szTmpPath[1024] = {0};  
  20.         sprintf(szTmpPath, "%s/%s", srcPath.c_str(), pDirent->d_name);  
  21.         destNewPath = destPath + "/" + pDirent->d_name;  
  22.   
  23.         if (IsDirectory(szTmpPath))  
  24.         {  
  25.             //如果是文件夹则进行递归  
  26.             iRet = IterFiles(szTmpPath, destNewPath);  
  27.             if (iRet < 0)  
  28.             {  
  29.                 break;  
  30.             }  
  31.         }  
  32.         else  
  33.         {/* do something */  
  34.         }  
  35.     }  
  36.   
  37.     closedir(pDir);  
  38.   
  39.     return iRet;  
  40. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值