遍历目录所有文件

 

原创,转载时请注明,谢谢。邮箱:tangzhongp@163.com

博客园地址:http://www.cnblogs.com/embedded-tzp

Csdn博客地址:http://blog.csdn.net/xiayulewa

   

Linux C : readdir

 

 

#include <stdio.h>

#include <dirent.h>

#include <stdlib.h>

 

int main(){

    DIR *dir_p = opendir("/");

    if(dir_p == NULL) perror("opendir"), exit(-1);

    struct dirent *ent;

    while(1){

        ent = readdir(dir_p);

        if(ent == NULL)  break;

        //打印子项类型和子项名

        if( 0 == strcmp(ent->d_name, ".")

         || 0 == strcmp(ent->d_name, "..")){

                continue;

        }   

        printf("%d, %s\n", ent->d_type, ent->d_name);

          //type == 4 是目录,其他是文件

    }

}

 

 

Linux C: scandir

 

 

#include <dirent.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

 

int dir_printall(const char *pathname)

{

    struct dirent **namelist = NULL;

    int ent_n;

    int i;

 

    ent_n = scandir(pathname, &namelist, NULL, alphasort);

    if (ent_n < 0){

        printf("scandir() fail : %s\n", pathname);

        return -1;

    }

    for(i = 0; i < ent_n; i++){

        if( 0 == strcmp(namelist[i]->d_name, ".")

                || 0 == strcmp(namelist[i]->d_name, "..")){ // skip parent dir and self

            continue;

        }

       

        char path_buf[512] = {0}; // use malloc will be bettor 

        sprintf(path_buf, "%s/%s", pathname, namelist[i]->d_name);

 

        printf("%s\n", path_buf);

 

        if(4 == namelist[i]->d_type){ // 4 means dir

            int retval = dir_printall( path_buf); // recurrence call

            if(-1 == retval){

                fprintf(stderr, "dir_printall() fail: %s\n", path_buf);

                continue;

                //goto out; // not end, for /proc/5236/net can't

            }

        }

    }

    

 

out:

    for(i = 0; i < ent_n; i++){

        if(namelist[i]){

            free(namelist[i]);

            namelist[i] = NULL;

        }

    }

    if(namelist){

        free(namelist);

        namelist = NULL;

    }

    return 0;

 

}

int main(void)

{

    if (-1 == dir_printall("/")){

        perror("dir_printall()");

    }

    return 0;

}

 

 

C++   

   

shell

     

#带完整路径

file=$(find ./)

echo $file

#只有文件名

file=$(ls -R)

echo $file

 

qt

自己做的项目中拷贝出来的一段简化的代码。

bool SearchThread::createDb()

{  

 qDebug() << __LINE__ << __FUNCTION__;

        QFileInfoList fileDriveList = QDir::drives(); // 获取盘符,windows下为c:, d:, e:, linux下为 /

        foreach(QFileInfo fileDrive, fileDriveList){ // 循环处理盘符

                qDebug() << fileDrive.absoluteFilePath();

                    createDb(fileDrive.absoluteFilePath());

        }   

        return true;

}

 

bool SearchThread::createDb(const QString &filePath)

{

        QDir dir = filePath;

 

const QDir::Filters FILTERS =  QDir::AllDirs | QDir::Files | QDir::Drives

                              | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System;

        QFileInfoList fileInfoList = dir.entryInfoList(FILTERS, QDir::DirsFirst | QDir::Name);

        foreach(QFileInfo fileInfo, fileInfoList){

                bool isdir = fileInfo.isDir();   

                if(isdir){

                        if(!fileInfo.isSymLink()){ // 不是链接文件,防止死循环

                            createDb(fileInfo.absoluteFilePath());

                        }

                }

        }

    return true;

}

 

【app】遍历目录所有文件的更多相关文章

  1. Linux下遍历目录及文件,更改权限

    Linux下遍历目录及文件,更改权限 引言: 我在Linux下搭建android时,将eclipse及sdk复制到/usr/下时,总会出现无法读,无法写写样的问题. 解决方案: 有两个方案: 一.将复 ...

  2. php遍历目录下文件,并读取内容

    <?php echo "<h2>遍历目录下文件,并读取内容</h2><br>\n"; function listDir($dir) { i ...

  3. dos下遍历目录和文件的代码(主要利用for命令)

    对指定路径指定文件进行遍历的程序,这里有多个批处理代码,但运行好像有些问题,大家可以根据需要选择 ===== 文件夹结构 ======================================= ...

  4. File类遍历目录及文件

    1. 构造函数 File(String args0)//使用一个表示文件或目录的路径的字符串创建一个File对象 File(URL args0)//使用一个URL对象创建File对象 File(Fil ...

  5. dos下遍历目录和文件的代码(主要利用for命令)(转)

    ===== 文件夹结构 ============================================= D:\test ---A Folder 1 |-----A file 1.txt | ...

  6. php遍历目录与文件夹的多种方法详解

    遍历目录或遍历目录下指定类型的文件,这是每一个童鞋在写程序的时候难免会用到的.PHP本身也提供了很多灰常有用的函数,正确地使用它们,不会有错滴.下面就我个人学习过程中的一些总结,希望对想学PHP的童鞋 ...

  7. Win32下C++遍历目录和文件的源码

    #include<windows.h> #include<iostream> #include<string> using namespace std; //只能处 ...

  8. PHP遍历目录和文件及子目录和文件

    正常直接使用opendir方法,就可以读到所有的目录和文件 文件可以直接记录下来,目录则需要再进一步获取里边的文件信息 也就是,如果当前读出来是目录,则需要再次调用函数本身(递归),直到没有目录 循环 ...

  9. linux c 遍历目录及文件

    #include <dirent.h>void recovery_backend() { DIR * pdir ; struct dirent * pdirent; struct stat ...

随机推荐

  1. Android:mimeType

    接收从其他应用传过来的数据,要用到清单文件 <activity android:name="com.terry.myActivity2" android:label=&quo ...

  2. Python 2.7 学习笔记 字典(map)的使用

    python中的字典,就是通常说的map,即 key/value集合的数据结构. 本文来介绍下在python下如何使用字典. 对于map这种数据结构能干什么,我们就不说了,这是一个常见的数据结构,我们 ...

  3. 17.1.1.5 Creating a Data Snapshot Using mysqldump 创建一个快照使用mysqldump:

    17.1.1.5 Creating a Data Snapshot Using mysqldump 创建一个快照使用mysqldump: 创建一个数据快照的方式是使用mysqldump 工具来备份所有 ...

  4. The connection to adb is down, and a severe error has occured.问题解决

    遇到问题描述: 运行android程序控制台输出 [2013-06-25 11:10:32 - MyWellnessTracker] The connection to adb is down, an ...

  5. 达内TTS6.0课件basic_day05

  6. 简单字符串处理 hdu2532 Engine

    本来可以把这篇文章放入上一篇文章里,不过做这个题花了一点时间,也有一点收获,同时觉得网上的这个题目可供参考的文章有些少,那么就单独成篇吧. 首先分析下题目思路: 这个题目是个模拟题,步骤也很清晰. 首 ...

  7. 如何在VMware中修改Mac OS的屏幕分辨率

    关于mac os分辨率问题:方法一:临时方法,只对当次启动有效,即在启动倒计时的时候,回车,等待输入参数是输入如下文本:“Graphics Mode"="1280x800x32@6 ...

  8. (step8.2.6)hdu 1848(Fibonacci again and again——组合博弈)

    题目大意:输入3个整数m,n,p,分别表示3堆石头中的石头个数 解题思路: 1)斐波那契数列的第16个数fib[16] == 1597 2)(sg[m]^sg[n]^sg[p])   .一定要加括号, ...

  9. O2O难解餐饮行业趋势下行之困

    近几年,O2O这个名词越来越常见,我们不但能够在IT相关资讯栏目看到它的存在,甚至在一些综合新闻版面也能轻易看到. 诚然.线下商家结合线上引流这样的方法,能够带来不少订单,可是O2O是否就能够解决餐饮 ...

  10. StreamWrite-StreamRead 读写文本文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...