遍历目录所有文件

 

原创,转载时请注明,谢谢。邮箱: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. Java-Math

    java.lang.Math类提供的方法都是static的,“静态引入 ”使得不必每次在调用类方法时都在方法前写上类名:             import static java.lang.Mat ...

  2. int_float_double数据类型的存储格式。

    一段用来检测编辑器存储方式的程序 //date : 2013/8/16 //designer :pengxiaoen //function check the C programmable langu ...

  3. 【转】Visual Stdio VS 错误 error : 0xC00000FD: Stack overflow. 更改堆栈空间解决栈溢出问题

    原文见:http://www.cnblogs.com/xiangwengao/archive/2012/03/16/2399888.html 问题 给一个程序添加小功能,在debug下能正常运行,在r ...

  4. Week12(11月28日)

    Part I:提问 =========================== 1.解读以下代码 $(document).ready(function(){    $('#btn1').click(fun ...

  5. WM_PAINT消息小结

    WM_PAINT是Windows窗口系统中一条重要的消息,应用程序通过处理该消息实现在窗口上的绘制工作. 1. 系统何时发送WM_PAINT消息? 系统会在多个不同的时机发送WM_PAINT消息:当第 ...

  6. About Us - Tech in Asia - Tech in Asia

    About Us - Tech in Asia - Tech in Asia About us Asia is big. Its place in the world, even bigger. Te ...

  7. BZOJ 3367: [Usaco2004 Feb]The Big Game 球赛( dp )

    dp(i)表示前i个人最少坐多少辆车, dp(i) = min(dp(j) + 1, dp(i)) (0 <= j < i 且 (i, j]的人能坐在一辆车上) 时间复杂度O(n²) -- ...

  8. 浅谈HTML之模仿人人网登陆界面(新手必学)

    为方便大家对web相关知识的了解,现谈谈新手如何从HTML css  Javascript到以后后台的发展.首先,让大家看看HTML仿人人登陆界面: <!doctype html> < ...

  9. jackson 转json. 过滤null值

    @Test public void tttttt() throws JsonGenerationException, JsonMappingException, IOException { Objec ...

  10. load data 方式导入的数据不可以用binlog日志进行恢复,因为binlog里面不产生insert sql语句。

    QQ群里面有人问起这个问题:    用load data 导入数据的时候,在binlog文件中记录的不是insert 语句,这样的话,如果用load data 导入数据,当需要恢复数据库的时候  bi ...