【app】遍历目录所有文件

遍历目录所有文件

 

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

}

 

转载于:https://www.cnblogs.com/embedded-tzp/p/4442147.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要移动一个 macOS 下的 Qt 应用程序目录下的所有文件到新目录,可以按照以下步骤进行操作: 1. 使用 QDir 类的 entryList() 函数获取应用程序目录下的所有文件和子目录。 2. 遍历所有文件和子目录,使用 QDir 类的 rename() 函数将其移动到新的目录中。 示例代码如下: ``` #include <QCoreApplication> #include <QDir> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QCoreApplication app(argc, argv); // 原始目录和目标目录路径 QString sourcePath = "/path/to/source/directory"; QString targetPath = "/path/to/target/directory"; // 获取原始目录下的所有文件和子目录 QDir sourceDir(sourcePath); QStringList files = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); // 遍历所有文件和子目录,移动到目标目录中 foreach (QString file, files) { QString sourceFile = sourcePath + "/" + file; QString targetFile = targetPath + "/" + file; if (!QFile::rename(sourceFile, targetFile)) { qDebug() << "Failed to move file: " << sourceFile << " -> " << targetFile; } } return app.exec(); } ``` 在上面的代码中,我们首先定义了原始目录和目标目录的路径。然后,使用 QDir 类的 entryList() 函数获取原始目录下的所有文件和子目录,并遍历它们。在遍历过程中,使用 QFile 类的 rename() 函数将文件和子目录移动到目标目录中。如果移动过程中出现错误,则使用 qDebug() 函数输出错误信息。 需要注意的是,如果目标目录已经存在同名文件或子目录,则移动操作会失败。如果需要覆盖目标文件或子目录,则需要先删除目标文件或子目录,然后再执行移动操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值