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] [ ...
随机推荐
- 【转】【selenium+Python WebDriver】之元素定位不到解决办法
感谢: 煜妃的<Python+Selenium定位不到元素常见原因及解决办法(报:NoSuchElementException)> ClassName定位报错问题:<[Python] ...
- java文本输入输出小结
Java 文本输入主要包含两种方法:FileRead -- 按字符读入,InputSreamReader -- 按行输入. java 文本输出也包含两种方法:FileWriter 和 OuputStr ...
- python 基础 7.6 sys 模块
一.sys 模块 sys 模块主要功能是获取参数 [root@www pythonscripts]# cat 2.py #!/usr/bin/python #coding=utf-8 im ...
- 继续封装DBDA.php 加入ajax
<?php class DBDA { public $host = "localhost"; //服务器地址 public $uid = "root"; ...
- toggle不支持事件代理的解决办法或者 jquery live绑定toggle
$(".xxx").live("click", function () { $(this).toggle(function () {},functio ...
- abap 数字移动小游戏
[转自 http://blog.csdn.net/forever_crazy/article/details/6542507] report ...... selection-screen pushb ...
- c# &与&& 和 |与||的区别(转载)
&:按位与,对两个条件都进行判断&&:逻辑与,只要一个条件满足,另外一个条件就不会执行 同理:|:按位或,对两个条件都进行判断||:逻辑或,只要一个条件满足,另外一个条件就不会 ...
- Zookeeper四字命令
ZooKeeper 支持某些特定的四字命令(The Four Letter Words)与其进行交互.它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息.用户在客户端可以通过 ...
- 在Spring中基于JDBC进行数据访问时如何控制超时
超时分类 超时根据作用域可做如下层级划分: Transaction Timeout > Statement Timeout > JDBC Driver Socket Timeout Tra ...
- Android NDK环境搭建
本文主要记录NDK环境在Ubuntu下的搭建. 下载NDK 在官网进行下载NDK https://developer.android.com/ndk/downloads/index.html 当前最新 ...