Windows和Linux下枚举文件夹下文件的方法
2016-05-17 14:54
429 查看
原文地址::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
DWORD IterFiles(string srcPath, string destPath)
{
DWORD dwStatus = 0;
WIN32_FIND_DATA findFileData;
string filePath = srcPath, srcNewPath = srcPath, destNewPath = destPath;
filePath += "\\*.*";
HANDLE hFind = FindFirstFile(filePath.c_str(), &findFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
srcNewPath = srcPath + "\\" + findFileData.cFileName;
destNewPath = destPath + "\\" + findFileData.cFileName;
if (strcmp(findFileData.cFileName, ".") == 0 || strcmp(findFileData.cFileName, "..") == 0)
{
continue;
}
else if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
dwStatus = IterFiles(srcNewPath, destNewPath);
}
else
{/* do something */
}
if (dwStatus != 0)
{
break;
}
} while (FindNextFile(hFind, &findFileData));
}
return dwStatus;
}
Linux
[cpp] view
plain copy
bool CLinDriverRW::IsDirectory(char *pszName)
{
struct stat S_stat;
//取得文件状态
if (lstat(pszName, &S_stat) < 0)
{
return false;
}
if (S_ISDIR(S_stat.st_mode))
{
return true;
}
else
{
return false;
}
}
[cpp] view
plain copy
DWORD IterFiles(string srcPath, string destPath)
{
int iRet = 0;
DIR *pDir;
string destNewPath;
struct dirent *pDirent;
pDir = opendir(srcPath.c_str());
if (pDir == NULL)
{
return -1;
}
while ((pDirent = readdir(pDir)) != NULL)
{
// if ((pDirent->d_name == '.') || pDirent->d_name[0] == '..')
if (strcmp(pDirent->d_name, ".") == 0 || strcmp(pDirent->d_name, "..") == 0)
{
continue;
}
char szTmpPath[1024] = {0};
sprintf(szTmpPath, "%s/%s", srcPath.c_str(), pDirent->d_name);
destNewPath = destPath + "/" + pDirent->d_name;
if (IsDirectory(szTmpPath))
{
//如果是文件夹则进行递归
iRet = IterFiles(szTmpPath, destNewPath);
if (iRet < 0)
{
break;
}
}
else
{/* do something */
}
}
closedir(pDir);
return iRet;
}
相关文章
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
DWORD IterFiles(string srcPath, string destPath)
{
DWORD dwStatus = 0;
WIN32_FIND_DATA findFileData;
string filePath = srcPath, srcNewPath = srcPath, destNewPath = destPath;
filePath += "\\*.*";
HANDLE hFind = FindFirstFile(filePath.c_str(), &findFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
srcNewPath = srcPath + "\\" + findFileData.cFileName;
destNewPath = destPath + "\\" + findFileData.cFileName;
if (strcmp(findFileData.cFileName, ".") == 0 || strcmp(findFileData.cFileName, "..") == 0)
{
continue;
}
else if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
dwStatus = IterFiles(srcNewPath, destNewPath);
}
else
{/* do something */
}
if (dwStatus != 0)
{
break;
}
} while (FindNextFile(hFind, &findFileData));
}
return dwStatus;
}
Linux
[cpp] view
plain copy
bool CLinDriverRW::IsDirectory(char *pszName)
{
struct stat S_stat;
//取得文件状态
if (lstat(pszName, &S_stat) < 0)
{
return false;
}
if (S_ISDIR(S_stat.st_mode))
{
return true;
}
else
{
return false;
}
}
[cpp] view
plain copy
DWORD IterFiles(string srcPath, string destPath)
{
int iRet = 0;
DIR *pDir;
string destNewPath;
struct dirent *pDirent;
pDir = opendir(srcPath.c_str());
if (pDir == NULL)
{
return -1;
}
while ((pDirent = readdir(pDir)) != NULL)
{
// if ((pDirent->d_name == '.') || pDirent->d_name[0] == '..')
if (strcmp(pDirent->d_name, ".") == 0 || strcmp(pDirent->d_name, "..") == 0)
{
continue;
}
char szTmpPath[1024] = {0};
sprintf(szTmpPath, "%s/%s", srcPath.c_str(), pDirent->d_name);
destNewPath = destPath + "/" + pDirent->d_name;
if (IsDirectory(szTmpPath))
{
//如果是文件夹则进行递归
iRet = IterFiles(szTmpPath, destNewPath);
if (iRet < 0)
{
break;
}
}
else
{/* do something */
}
}
closedir(pDir);
return iRet;
}
相关文章推荐
- linux如何关闭防火墙
- Linux命令学习-图形化界面命令开关闭
- Linux top 命令
- part1:15-安装Linux系统到开发板
- Linux,执行.sh文件,提示No such file or directory
- CPU的寻址方式(Linux内核情景分析)
- linux和windows的换行符问题
- linux后台开发必备技能
- Linux 下 Error: Could not find or load main class Hello world
- Solution of wireless link "PCI unknown" on Centos 7.1
- linux grep命令详解
- linux系统中的定时器
- CentOS6.5分区与文件系统
- Linux命令 - netstat
- part1:13-linux编译器GCC
- linux tar命令运维实践
- Linux的防火墙--IP Tables
- linux系统命令大全使用详解(大全)
- 调优HASHSIZE和CONNTRACK_MAX值
- 使用screen管理linux会话