W3cubDocs

/C++

std::filesystem::directory_entry::is_directory

bool is_directory() const;
bool is_directory( std::error_code& ec ) const noexcept;
(since C++17)

Checks whether the pointed-to object is a directory. Effectively returns std::filesystem::is_directory(status()) or std::filesystem::is_directory(status(ec)), respectively.

Parameters

ec - out-parameter for error reporting in the non-throwing overload

Return value

true if the referred-to filesystem object is a directory, false otherwise.

Exceptions

The overload that does not take a std::error_code& parameter throws filesystem::filesystem_error on underlying OS API errors, constructed with p as the first path argument and the OS error code as the error code argument. The overload taking a std::error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.

Example

#include <iostream>
#include <filesystem>
 
namespace fs = std::filesystem;
 
int main()
{
    fs::directory_entry d1(".");
    fs::directory_entry d2("file.txt");
    fs::directory_entry d3("new_dir");
 
    std::cout << std::boolalpha
              << ". d1 " << d1.is_directory() << '\n'
              << "file.txt d2 " << d2.is_directory() << '\n'
              // false because it has not been created
              << "new_dir d3 " << d3.is_directory() << '\n';
 
    fs::create_directory("new_dir");
 
    std::cout << "new_dir d3 before refresh " << d3.is_directory() << '\n';
    d3.refresh();
    std::cout << "new_dir d3 after refresh " << d3.is_directory() << '\n';
}

Possible output:

. d1 true
file.txt d2 false
new_dir d3 false
new_dir d3 before refresh false
new_dir d3 after refresh true

See also

(C++17)
checks whether the given path refers to a directory
(function)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/filesystem/directory_entry/is_directory