C++ 仅列出目录中的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5043403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:15:18  来源:igfitidea点击:

Listing only folders in directory

c++posix

提问by m4tx

I want to list folders in a directory in C++, ideally in a portable (working in the major Operating Systems) way. I tried using POSIX, and it works correctly, but how can i identify whether the found item is a folder?

我想在 C++ 中列出目录中的文件夹,最好以可移植的方式(在主要操作系统中工作)。我尝试使用 POSIX,它工作正常,但是我如何确定找到的项目是否是文件夹?

采纳答案by wally

Using the C++17 std::filesystemlibrary:

使用 C++17std::filesystem库:

std::vector<std::string> get_directories(const std::string& s)
{
    std::vector<std::string> r;
    for(auto& p : std::filesystem::recursive_directory_iterator(s))
        if (p.is_directory())
            r.push_back(p.path().string());
    return r;
}

回答by davidag

You could use opendir()and readdir()to list directories and subdirectories. The following example prints all subdirectories inside the current path:

您可以使用opendir()readdir()列出目录和子目录。以下示例打印当前路径内的所有子目录:

#include <dirent.h>
#include <stdio.h>

int main()
{
    const char* PATH = ".";

    DIR *dir = opendir(PATH);

    struct dirent *entry = readdir(dir);

    while (entry != NULL)
    {
        if (entry->d_type == DT_DIR)
            printf("%s\n", entry->d_name);

        entry = readdir(dir);
    }

    closedir(dir);

    return 0;
}

回答by Beno?t

Here follows a (slightly modified) quote from the boost filesystem documentationto show you how it can be done:

下面是来自boost 文件系统文档的(稍微修改的)引用,向您展示如何完成它:

void iterate_over_directories( const path & dir_path )         // in this directory,
{
  if ( exists( dir_path ) ) 
  {
    directory_iterator end_itr; // default construction yields past-the-end
    for ( directory_iterator itr( dir_path );
          itr != end_itr;
          ++itr )
    {
      if ( is_directory(itr->status()) )
      {
        //... here you have a directory
      }
    }
  }
}

回答by Graeme Perrow

Look up the statfunction. Hereis a description. Some sample code:

stat函数。这里有一个说明。一些示例代码:

struct stat st;
const char *dirname = "dir_name";
if( stat( dirname, &st ) == 0 && S_ISDIR( st.st_mode ) ) {
    // "dir_name" is a subdirectory of the current directory
} else {
    // "dir_name" doesn't exist or isn't a directory
}

回答by James

I feel compelled to mention PhysFS. I just integrated it into my own project. It provides true cross-platform (Mac / Linux / PC) file operations and can even unpack various archive definitions such as zip, 7zip, pak, and so on. It has a few functions (PHYSFS_isDirectory, PHYSFS_enumerateFiles) which can determine what you are asking for as well.

我觉得不得不提到PhysFS。我只是将它集成到我自己的项目中。它提供了真正的跨平台(Mac / Linux / PC)文件操作,甚至可以解压各种压缩文件定义,例如 zip、7zip、pak 等。它有一些功能(PHYSFS_isDirectoryPHYSFS_enumerateFiles),它们也可以确定您的要求。

回答by Jeremy Friesner

Under Windows, you can use _findfirst() and _findnext() to iterate through the contents of a directory, and then use CreateFile() and GetFileInformationByHandle() to determine whether a particular entry is a directory or a folder. (Yes, CreateFile(), with the appropriate arguments, to examine an existing file. Ain't life grand?)

在 Windows 下,您可以使用 _findfirst() 和 _findnext() 遍历目录的内容,然后使用 CreateFile() 和 GetFileInformationByHandle() 确定特定条目是目录还是文件夹。(是的,使用适当的参数 CreateFile() 来检查现有文件。是不是很美好?)

For reference, some classes where I implemented code that uses those calls can be seen hereand here

作为参考,我实现了使用这些调用的代码的一些类可以在这里这里看到