CKTIS / C++
std::filesystem::create_directories: creating a complete directory path
std::filesystem::create_directories creates missing directories along a path in order and reports whether it created the final directory.
Version: C++23
Syntax
bool std::filesystem::create_directories(const std::filesystem::path& p);
bool std::filesystem::create_directories(
const std::filesystem::path& p, std::error_code& ec);std::filesystem::create_directories(p) from <filesystem> invokes the equivalent of create_directory for every nonexistent element of p. It returns true only when the directory resolved by the complete p was created; if that directory already existed it returns false without an error.
The example creates nested alpha/beta, repeats the call, and prints 1 0 1: the first call created the target, the second changed nothing, and the final directory exists.
This is not a transaction: if a later element fails, earlier directories can remain. An existing non-directory component, insufficient permission, or an invalid path reports an error. The throwing overload uses filesystem_error; the error_code& ec form reports through ec but is not declared noexcept in C++23.
Examples
#include <filesystem>
#include <iostream>
int main() {
namespace fs = std::filesystem;
const fs::path root = fs::temp_directory_path() / "cktis-cpp-ref-create-directories";
const fs::path nested = root / "alpha" / "beta";
std::error_code ignored;
fs::remove_all(root, ignored);
const bool first = fs::create_directories(nested);
const bool second = fs::create_directories(nested);
std::cout << first << ' ' << second << ' ' << fs::is_directory(nested) << '\n';
fs::remove_all(root, ignored);
}
Result
1 0 1
std::filesystem::create_directories → 1 0 1