【发布时间】:2012-02-26 16:10:24
【问题描述】:
我是 c++ 新手。有人可以给我一些关于如何在 LINUX 中递归获取所有目录及其所有子目录的代码。我在互联网上没有找到任何可以帮助我的东西(或有效的代码。)我需要获取文件夹及其子文件夹中的所有文件。
在 UBUNTU 中我没有 getfiles、目录...
【问题讨论】:
标签: c++ linux ubuntu directory
我是 c++ 新手。有人可以给我一些关于如何在 LINUX 中递归获取所有目录及其所有子目录的代码。我在互联网上没有找到任何可以帮助我的东西(或有效的代码。)我需要获取文件夹及其子文件夹中的所有文件。
在 UBUNTU 中我没有 getfiles、目录...
【问题讨论】:
标签: c++ linux ubuntu directory
在 Linux 上试试这个:
#include <iostream>
#include <string>
#include <dirent.h>
void ProcessDirectory(std::string directory);
void ProcessFile(std::string file);
void ProcessEntity(struct dirent* entity);
std::string path = "/path/to/directory/";
int main()
{
std::string directory = "theDirectoryYouWant";
ProcessDirectory(directory);
return 0;
}
void ProcessDirectory(std::string directory)
{
std::string dirToOpen = path + directory;
auto dir = opendir(dirToOpen.c_str());
//set the new path for the content of the directory
path = dirToOpen + "/";
std::cout << "Process directory: " << dirToOpen.c_str() << std::endl;
if(NULL == dir)
{
std::cout << "could not open directory: " << dirToOpen.c_str() << std::endl;
return;
}
auto entity = readdir(dir);
while(entity != NULL)
{
ProcessEntity(entity);
entity = readdir(dir);
}
//we finished with the directory so remove it from the path
path.resize(path.length() - 1 - directory.length());
closedir(dir);
}
void ProcessEntity(struct dirent* entity)
{
//find entity type
if(entity->d_type == DT_DIR)
{//it's an direcotry
//don't process the '..' and the '.' directories
if(entity->d_name[0] == '.')
{
return;
}
//it's an directory so process it
ProcessDirectory(std::string(entity->d_name));
return;
}
if(entity->d_type == DT_REG)
{//regular file
ProcessFile(std::string(entity->d_name));
return;
}
//there are some other types
//read here http://linux.die.net/man/3/readdir
std::cout << "Not a file or directory: " << entity->d_name << std::endl;
}
void ProcessFile(std::string file)
{
std::cout << "Process file : " << fileToOpen.c_str() << std::endl;
//if you want to do something with the file add your code here
}
【讨论】:
递归是不必要的。 Linux 上有一个工具可以迭代地执行此操作。
#include <ftw.h>
int ftw(const char *dirpath,
int (*fn) (const char *fpath, const struct stat *sb,
int typeflag),
int nopenfd);
ftw() 函数为给定树中的每个文件和目录调用提供的回调。
【讨论】:
【讨论】:
除了Dmitri's answer,您可能对使用“为您进行递归”的nftw 库函数感兴趣
【讨论】:
使用nftw。它提供了各种选项来微调目录遍历。
该页面还显示an example。
【讨论】:
列出目录的答案只是答案的第一部分,但我没有看到有人回答递归部分。要递归地做任何事情,你必须创建一个“调用自身”的子例程——注意在处理符号链接时应该小心,尤其是在使用 nfs 时像 /exports 这样的情况下,这可能会导致循环递归并将你锁定在无限中环形!基本上:
这不是真正的代码,它的伪代码试图帮助您获得更好的想法 递归是如何工作的,而不会使您对语言感到困惑,这个想法可以 适用于任何具有某种调用返回机制的语言 这些天几乎是我能想到的每一种语言
// PSEUDO-CODE
stiring entriesarray[] myfunc(string basedir)
{
string results[] = getfilesandfolders(basedir) // you might want to specify a filter
for each string entry in results
{
// if you want only files you'll need to test for if entry is a file
entriesarray.add(entry)
if (entry is a directory && entries is not a symbolic link)
{
string moreentriesarray[] = myfunc(entry)
entriesarray.join(moreentriesarray)
}
}
return entriesarray[]
}
注意如果条目不包含任何真正的目录,函数不会调用自身?这很重要,因为这是避免无限递归的方式。但请注意,您可能希望这样做,以便可以取消此操作,更大的文件系统 这些天可能需要很多时间来处理。我通常这样做的方式是开始另一个 线程并在后台进行搜索,并让后台线程检查 取消标志,以防用户想要停止操作,因此它可以发布有关剩余时间、完成百分比等信息。它有点粗糙但是 这应该会让任何对这种事情感兴趣的人朝着正确的方向前进。和 请记住始终正确检查错误并进行异常处理,这是 我看到新程序员总是跳过。
【讨论】: