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] [ ...
随机推荐
- Socket的UDP协议在erlang中的实现
现在我们看看UDP协议(User Datagram Protocol,用户数据报协议).使用UDP,互联网上的机器之间可以互相发送小段的数据,叫做数据报.UDP数据报是不可靠的,这意味着如果客户端发送 ...
- Til the Cows Come Home(最短路模板题)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description Bessie is ...
- SQL Server 中 GO 的用法(转)
本科里学了那么多年SQL Server一直看到书上各种SQL语句中间夹杂着那么几个看似毫无意义的GO,看着就让人莫名,问老师,老师一般只会告诉你,不要理他,这个东西没用的.但是个性纠结并且有轻微强迫症 ...
- python cookbook第三版学习笔记七:python解析csv,json,xml文件
CSV文件读取: Csv文件格式如下:分别有2行三列. 访问代码如下: f=open(r'E:\py_prj\test.csv','rb') f_csv=csv.reader(f) for f in ...
- .net概念(转)
你主要想问.Net和Java的差异在哪里 Java是开发语言 .Net叫开发平台 但事实上你管Java叫开发平台也没错 平台就是一个供你在上面进行开发的平台 (英语叫Framework,也可以翻译成“ ...
- [IR课程笔记]Web search
一. 搜索引擎 组成部分: 1. 网络爬虫(web crawler) 2. 索引系统(indexing system) 3. 搜索系统 (searching system) consideratio ...
- Machine Learning No.11: Recommender System
1. Content based Problem formulation Content Based Recommendations: 2. collaborative filtering algor ...
- sudo执行提示Command not found
运行一命令在普通用户下可行,切换到root用户依然可行,但在普通用户下使用sudo执行时,提示Command not found. 修改/etc/sudoers文件,找到类似下面的一行: Defaul ...
- importlib模块 反射字符串的对象
通过 importlib模块 反射字符串的对象 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux ...
- 经典数学问题<手电过河问题>的动态解法--问题规模扩展至任意大小
非常有趣的一件事是今天在TopCoder的1000分题里面发现了这道经典数学问题. Notes - In an optimal solution ...