#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h> using std::strcmp;
using std::string;
using std::vector; #if __cplusplus < 201103L
#define nullptr NULL
#endif #define SEARCH_PATH_LENGTH 1024 /*
* 搜索一个文件夹下的符合条件的文件
*/
bool searchFile(const char* directory, vector<string>& files, const char* ext, int subdir_level)
{
const char* dir_path = directory; DIR *dir = nullptr; //打开文件夹
if((dir = opendir(dir_path)) == nullptr)
{
return false;
} struct dirent entry;
struct dirent* result;
struct stat file_stat; char cur_name[SEARCH_PATH_LENGTH]; //读取文件夹下的一个文件
while(readdir_r(dir, &entry, &result) == 0 && result)
{
const char* name = entry.d_name; //忽略掉.和..
if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
continue; //检查拼接路径
int count = sprintf(cur_name, "%s", dir_path); count += sprintf(&cur_name[count], "%s%s", cur_name[count - 1] == '/' ? "" : "/", name); //获取文件的状态
if(lstat(cur_name, &file_stat) != -1)
{
//文件是普通文件
if(!S_ISDIR(file_stat.st_mode))
{
const char* rf = strrchr(cur_name, '.'); //检查文件的后缀名
if(rf != nullptr && strcmp(rf + 1, ext) == 0)
files.push_back(string(cur_name, count));
}
//文件是文件夹
else if(subdir_level >= 0)
{
//递归获取下一层目录的文件
searchFile(cur_name, files, ext, subdir_level - 1);
}
}
}
closedir(dir); return true;
}

C++ 查找文件夹下的文件的更多相关文章

  1. Linux 批量查找并替换文件夹下所有文件的内容

    1.批量查找某个目下文件的包含的内容 cd etc grep -rn "查找的内容" ./ 2.批量替换某个目下所有包含的文件的内容 cd etc sed -i "s/查 ...

  2. C#遍历文件夹下所有文件

    FolderForm.cs的代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using ...

  3. python 替换 文件夹下的 文件名称 及 文件内容

    示例效果: 1.替换某文件夹下的 文件夹及子文件夹 的名称 由OldStrDir 变为 NewStrDir: 2.替换某文件夹下的 文件夹及子文件夹 下 所有的文件的名称 由OldStrFile 变为 ...

  4. java笔试题: ——将e:/source文件夹下的文件打个zip包后拷贝到f:/文件夹下面

    将e:/source文件夹下的文件打个zip包后拷贝到f:/文件夹下面 import java.io.*; import java.util.zip.ZipEntry; import java.uti ...

  5. 【转发】du命令 实现Linux 某个文件夹下的文件按大小排序

    1. df -lh 2. du -s /usr/* | sort -rn这是按字节排序 3. du -sh /usr/* | sort -rn这是按兆(M)来排序 4.选出排在前面的10个du -s ...

  6. java读取文件夹下所有文件并替换文件每一行中指定的字符串

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...

  7. python (9)统计文件夹下的所有文件夹数目、统计文件夹下所有文件数目、遍历文件夹下的文件

    命令:os 用到的:os.walk   os.listdir 写的爬虫爬的数据,但是又不知道进行到哪了,于是就写了个脚本来统计文件的个数 #统计 /home/dir/ 下的文件夹个数 import o ...

  8. PHP遍历文件夹下的文件和获取到input name的值

    <?php$dir = dirname(__FILE__); //要遍历的目录名字 ->当前文件所在的文件夹//$dir='D:\PHP\wamp\www\admin\hosts\admi ...

  9. java中File类应用:遍历文件夹下所有文件

    练习: 要求指定文件夹下的所有文件,包括子文件夹下的文件 代码: package 遍历文件夹所有文件; import java.io.File; public class Test { public ...

  10. 【转】【Linux】Linux下统计当前文件夹下的文件个数、目录个数

    [转][Linux]Linux下统计当前文件夹下的文件个数.目录个数 统计当前文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 统计文件夹下目录的 ...

随机推荐

  1. JSON对象与JSON数组的长度和遍历方法

    JSON对象与JSON数组的长度和遍历方法         1.json对象的长度与遍历                 结构:var json={“name”:”sm”,”sex”:”woman”} ...

  2. chart.js 示例

    一个简单的例子. 1.html代码 <div id="pie" style="width: 250px;float:left"> <h3> ...

  3. Oracle遇到的一些问题

    1.制造数据时出现错误 ORA-30009问题 计划任务: insert into test select rownum,sysdate from dual connect 会出现ORA-30009 ...

  4. 帝国cms7.0整合百度编辑器ueditor教程

    帝国cms7.0整合百度编辑器ueditor教程开始 1.根据自己使用的帝国cms版本编码下载对应的ueditor版本 下载地址 http://ueditor.baidu.com/website/do ...

  5. hadoop学习之hadoop完全分布式集群安装

    注:本文的主要目的是为了记录自己的学习过程,也方便与大家做交流.转载请注明来自: http://blog.csdn.net/ab198604/article/details/8250461 要想深入的 ...

  6. JQuery基础学习总结

    JQuery基础学习总结 简单总结下JQuery: 一:事件 1.change事件 <!DOCTYPE html> <html lang="en"> < ...

  7. 学习ReactNative笔记整理一___JavaScript基础

    学习ReactNative笔记整理一___JavaScript基础 ★★★笔记时间- 2017-1-9 ★★★ 前言: 现在跨平台是一个趋势,这样可以减少开发和维护的成本.第一次看是看的ReactNa ...

  8. apache window环境下本地配置虚拟主机

    1.在httpd.conf中去掉如下注释: # Virtual hostsInclude conf/extra/httpd-vhosts.conf 2.在 httpd-vhosts.conf添加内容 ...

  9. 关于Android界面编程与视图(View)组件

    UI组件--------------->android.widget.* View组件------------->android.view.* 视图(View)组件 所有UI组件都是建立在 ...

  10. HttpStatusCode 枚举

    .NET Framework 类库 HttpStatusCode 枚举 包含为 HTTP 定义的状态代码的值. 命名空间:System.Net程序集:System(在 system.dll 中)   ...