#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. hdu 1009 贪心基础题

    B - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bi ...

  2. Windows环境变量修改

    做开发的时候都设置过Windows的环境变量,一直觉得实在是难用之极.网上搜索过解决方案,有个开源的小程序,试过,有问题,没有办法正常运行.所以自己写一个,权当练手. 下载地址:http://file ...

  3. [C入门 - 游戏编程系列] 贪吃蛇篇(五) - 蛇实现

    因为已经写了食物的实现,所以我不知道到底是该先写世界的实现还是蛇的实现.因为世界就是一个窗口,可以立刻在世界中看到食物的样子,对于大多数人来说,如果写完代码立刻就能看到效果,那就再好不过了.可是,我最 ...

  4. 开心菜鸟系列学习笔记------javascript(5)

    一.this的关系    1)全局代码中的this    2)函数代码中的this在函数代码中使用this时很有趣,这种情况很难且会导致很多问题. 这种类型的代码中,this值的首要特点(或许是最主要 ...

  5. cf C. Cupboard and Balloons

    http://codeforces.com/contest/342/problem/C #include <cstdio> #include <cstring> #includ ...

  6. IC封装图片认识(二):SOP&SOJ

    SOP SOP-EIAJ-TYPE-II-14L SSOP SSOP-16L TSOP(Thin Small Outline Package) TSSOP(Thin Shrink Outline Pa ...

  7. SATA1.0,2.0,3.0区别

    外观没区别,接口都一样,线也一样,就是传输速率不一样,控制芯片不一样SATA1.0理论传输速度为1.5Gbit/s SATA2.0理论传输速度为3Gbit/sSATA2.0理论传输速度为6Gbit/s ...

  8. 使用linux系统做路由转发

    使用linux系统(PC机)做路由转发 关键字:linux,Fedora,route,iptables,ip_forward 最近做网络实验,在实验过程中需要用到linux的转发功能,但是遇到一些问题 ...

  9. 浅谈JS数据类型存储问题

    背景       一个经典的问题,先抛出来给大伙看看: var a = "黑MAO"; var b = a; var c = new Object(); var d = c; a ...

  10. handsontable的单元格操作方法

    1.为handsontable添加钩子方法 addHook(key,callback):key为钩子方法名 <span style="font-size:18px;"> ...