linux 遍历目录+文件(优化版本)
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/time.h> //linux 精确度s, us gettimeofday()
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//char filename[512];
std::vector<string> vecFilePath;
std::vector<string> vecFilename;
int isDirectory(string fullpath)
{
struct stat st;
int ret = stat(fullpath.c_str(), &st);//return -1, if failed
if(ret==0)//ok
{
if(S_ISDIR( st.st_mode))//是文件目录
{
//cout<<"S_ISDIR:"<<fullpath<<endl;
return 1;
}
else if(S_ISREG (st.st_mode) ) //是文件
{
//cout<<"S_ISREG:"<<fullpath<<endl;
return 0;
}
}
return -1;//failed
}
int TraverseDir(string path, bool isNeedFilterFlag, vector<string> vecSuffix,bool isTraveSubDirFlag)
{
DIR *d; //声明一个句柄
struct dirent *file; //readdir函数的返回值就存放在这个结构体中
if(!(d = opendir(path.c_str())))
{
cout<< "error opendir:"<<path<<endl;
return -1;
}
while((file = readdir(d)) != NULL)
{
string strFileName(file->d_name);
//隐藏文件.a.txt 隐藏目录.folderA, 没做处理,可以被找到。
//忽略 当前目录. 和上一级目录..
//if(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)
if(strFileName.compare(".")==0 || strFileName.compare("..")==0)
{
continue;
}
string fullPath = path + "/" + strFileName;// path+"//"+strFileName; 也OK
int ret = isDirectory(fullPath);
if(ret==0)//是文件
{
if(isNeedFilterFlag==true)//需要过滤
{
bool isNeedSuffix(0);
for(uint32_t i =0; i< vecSuffix.size(); i++)
{
string fileSuffix = fullPath.substr(fullPath.length() - vecSuffix.at(i).length());
bool bMatch = fileSuffix.compare(vecSuffix.at(i))==0;
isNeedSuffix = isNeedSuffix || bMatch;
}
if(isNeedSuffix)
{
vecFilename.push_back(strFileName);
// vecFilename.push_back(fullPath);
}
}
else
{
vecFilename.push_back(strFileName);
}
}
else if(ret==1)//是目录
{
if(isTraveSubDirFlag==true)//如果需要可以继续遍历
{
TraverseDir(fullPath, isNeedFilterFlag, vecSuffix, isTraveSubDirFlag);
}
//continue;
}
else //stat() return -1, failed
{
continue;
}
}
closedir(d);
return 0;
}
int main()
{
string strPath("/home/scott/project"); // /home/scott/project/data
vector<string> vecSuffix;
vecSuffix.push_back(".jpg");
vecSuffix.push_back(".png");
vecSuffix.push_back(".aac");
vecSuffix.push_back(".mp3");
vecSuffix.push_back(".wma");
vecSuffix.push_back(".wave");
vecSuffix.push_back(".mp4");
vecSuffix.push_back(".rmvb");
vecSuffix.push_back(".rmvb");
vecSuffix.push_back(".mov");
vecSuffix.push_back(".flv");
vecSuffix.push_back(".ts");
bool isNeedFilterFlag(true);//过滤文件类型
bool isTraveSubDirFlag(true);//false
struct timeval tvBegin,tvEnd;
struct timezone tz;
gettimeofday (&tvBegin , &tz);
TraverseDir(strPath, isNeedFilterFlag, vecSuffix, isTraveSubDirFlag);
gettimeofday (&tvEnd , &tz);
uint64_t SpendTime = (tvEnd.tv_sec-tvBegin.tv_sec)*1000+(tvEnd.tv_usec-tvBegin.tv_usec)/1000;
std::cout<< "tvBegin.tv_sec ="<<tvBegin.tv_sec << ".tvBegin.tv_usec ="<< tvBegin.tv_usec<<std::endl;
std::cout<< "tvEnd.tv_sec ="<<tvEnd.tv_sec << ".tvEnd.tv_usec ="<< tvEnd.tv_usec <<std::endl;
std::cout<< "SpendTime="<<SpendTime<<"ms"<<std::endl;
cout<<"find number:"<<vecFilename.size()<<endl;
/*
for(int i = 0; i < vecFilename.size(); i++)
{
cout<< vecFilename.at(i)<<endl;
}*/
return 0;
}
linux 遍历目录+文件(优化版本)的更多相关文章
- Linux 遍历目录下面所有文件,将目录名、文件名转为小写
当你从 Windows 服务器换到 Linux 服务器的时候,以前的上传目录的目录名.文件名会遇到大小写的问题.在 Windows 环境下面没有文件区分大小写的概念,而 Linux 却有严格的文件名大 ...
- python遍历目录文件脚本的示例
例子 自己写的一个Python遍历文件脚本,对查到的文件进行特定的处理.没啥技术含量,但是也记录一下吧. 代码如下 复制代码 #!/usr/bin/python# -*- coding: utf-8 ...
- c#调用api(FindFirstFile,FindNextFile)高效遍历目录文件【转载】
在c#下遍历目录,应用最多的应该就是 System.IO.DirectoryInfo.GetDirectories或GetFiles了,但是当目录特别大,文件特别多时,效率不尽人意,此时我们很容易想到 ...
- 【Linux】目录文件权限的查看和修改【转】
转载自:http://zhaoyuqiang.blog.51cto.com/6328846/1214718 ============================================== ...
- ZH奶酪:PHP遍历目录/文件的3种方法
其实PHP中内建函数scandir()就可以返回目录下全部文件和目录了... ========================== 1.使用$obj = dir($dir)返回目录对象$obj,然后使 ...
- Linux】目录文件权限的查看和修改【转】
转载自:http://zhaoyuqiang.blog.51cto.com/6328846/1214718 ============================================== ...
- linux 修改目录文件权限,目录文件所属用户,用户组
1:查看命令:ll drwxr-xr-x 4 gamer ftp 4096 Mar 7 16:56 gstore drwxrwxrwx 10 root ftp 4096 De ...
- linux遍历目录源代码
<pre code_snippet_id="1622396" snippet_file_name="blog_20160324_1_744516" nam ...
- linux查看目录文件以及子目录文件大小的命令
可以使用以下命令,不过如果文件比较多,因为是递归统计大小的的,所以结果出来的会比较慢,需要等待. du -h --max-depth=1 * 以下是命令的说明 du [-abcDhHklmsSx] [ ...
随机推荐
- PowerBuilder -- Len(), LenA() 与 String, Blob
使用的是Powerbuilder12.5与Powerbuild9 不太一样 函数 String Blob Len() 返回字符数 返回字符数对应的字节数 LenA() 返回字节数 返回字符数对应的字节 ...
- C#使用for循环移除HTML标记
public static string StripTagsCharArray(string source) { char[] array = new char[source.Length]; int ...
- TP框架---thinkphp基础知识
php框架 发瑞 一.真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困 ...
- python 基础及资料汇总
Python 包.模块.类以及代码文件和目录的一种管理方案 Numpy 小结 用 Python 3 的 async / await 做异步编程 K-means 在 Python 中的实现 ...
- DNS--域名系统 随笔
定义:是一种用于TCP/IP应用程序的分布式数据库.(分布式数据库:指利用高速计算机网络将物理上分散的多个数据存储单元连接起来组成一个逻辑上统一的数据库.分布式数据库的基本思想是将原来集中式数据库中的 ...
- 带有button而且能够运行单击事件的WINFORM窗口,体悟C#的创建过程
using System; using System.Drawing; using System.Windows.Forms; namespace Window{ class Window{ stat ...
- YTST_CX_0001(ALV栏位汇总)
*********************************************************************** * Title : X ...
- touchweb手机网站图片加载方法(canvas加载和延迟加载)
一.canvas图片加载 关于canvas加载,我的方法是,将文章中所有用到图片的地方,都用canvas代替,给canvas一个data-src,里面存放img的路径,通过canvas方法渲染图片.因 ...
- MVC+Ext.net零基础学习记录(四)
在上一篇文章[MVC+Ext.net零基础学习记录(三)]中提到了利用MVC的Area可以做到项目分离,但是实际操作起来还是有很多问题的.比如,对于物理资源的访问,会报:没有相关资源 开始的时候,我在 ...
- LightOJ - 1321 Sending Packets —— 概率期望
题目链接:https://vjudge.net/problem/LightOJ-1321 1321 - Sending Packets PDF (English) Statistics Foru ...