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] [ ...
随机推荐
- Android 实现的EditText响应drawableRight的点击事件
1.自定义Edittext 实现右侧图标点击清空 package com.dxw.live.view; import android.content.Context; import android.g ...
- python 基础 5.0 python类一般形式
一. 类的一般形式 创建类我们一般使用class 关键字来创建一个类,class 后面跟类型名字,可以自定义,最后以冒号结尾,如下所示: #/usr/bin/python #coding=utf- ...
- 开发环境部署git 步骤
1.安装完后,打开git bash. vim .gitconfig 此文件放在用户目录,即 git bash 打开后默认位置. 2.插入以下内容,保存 [user] name = email = [ ...
- 高性能流媒体服务器EasyDSS前端重构(二) webpack + vue + AdminLTE 多页面提取共用文件, 优化编译时间
本文围绕着实现EasyDSS高性能流媒体服务器的前端框架来展开的,具体EasyDSS的相关信息可在:www.easydss.com 找到! 接上回 <高性能流媒体服务器EasyDSS前端重构(一 ...
- android菜鸟学习笔记14----Android控件(三) ListView的简单使用
MVC模式: MVC的基本原理就是通过Controller连接View和Model.当View中所显示的数据发生变化时,会通知Controller,然后由Controller调用Model中的相关方法 ...
- Tomcat学习笔记【2】--- Tomcat安装、环境变量配置、启动和关闭
本文主要讲Tomcat的安装和配置. 一 Tomcat安装 1.1 下载 下载地址:http://tomcat.apache.org/ 1.2 安装 Tomcat是不需要安装的,解压压缩包即可. 在安 ...
- spring运行步骤
Spring确实使你能通过最简单可行的解决的方法来解决你的问题. 而这是有有非常大价值的.同一时候他的源码的设计理念也受到非常多程序猿的追捧,简洁,易用.但是从哪着手研究Spring却是非常多新手头疼 ...
- 公司IIS 项目公布 注意点
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/sat472291519/article/details/24010721 IIS - 右键 - 属性 ...
- clone和dup
ruby中clone和dup都是对一个对象的浅拷贝,其区别如下: 1.clone会拷贝单例方法,而dup不会. a = Object.new def a.hello "hello" ...
- A. Playing with Paper
这是Codeforces Round #296 (Div. 2)的A题,题意就是: 小明有一张长为a,宽为b的纸,每当要折纸鹤时,就从纸上剪下一个正方形,然后,剩下的纸还可以剪出正方形,要是剩下的纸刚 ...