When you try to create a directory that already exists using std::filesystem::create_directory()
, the function will return false
. It does not throw an exception in this case.
However, if you use std::filesystem::create_directories()
, it will create the entire directory path if any part of it does not exist and will not throw an error if the directory already exists.
Here’s an example to illustrate this behavior:
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main() {
fs::path dir_path{R"(c:\test)"};
try {
bool created = fs::create_directory(dir_path);
if (created) {
std::cout << "Directory created";
} else {
std::cout << "Directory already exists";
}
} catch (fs::filesystem_error &e) {
std::cerr << e.what() << '\n';
}
}
Directory already exists
In this example:
dir_path
as the path to the directory we want to create.fs::create_directory()
and check its return value.create_directory()
returns false
, and we print "Directory already exists."To handle scenarios where you might need to create a nested directory structure, you can use fs::create_directories()
:
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main() {
fs::path nested_dir_path{R"(c:\test\subdir)"};
try {
bool created =
fs::create_directories(nested_dir_path);
if (created) {
std::cout << "Directories created";
} else {
std::cout << "Directories already exist";
}
} catch (fs::filesystem_error &e) {
std::cerr << e.what();
}
}
Directories already exist
In this case:
fs::create_directories()
creates all necessary parent directories if they do not exist.false
without throwing an exception.Using fs::create_directories()
is particularly useful when you want to ensure the entire path exists without worrying about whether each individual directory exists.
Both functions handle errors gracefully, so you should always wrap these calls in a try
block and catch any fs::filesystem_error
exceptions to handle other potential issues, such as permission errors or invalid paths.
If you want to overwrite an existing directory, you will need to delete the existing directory and then create a new one. Here’s how you can do it:
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main() {
fs::path dir_path{R"(c:\test)"};
try {
if (fs::exists(dir_path)) {
fs::remove_all(dir_path);
std::cout << "Existing directory removed\n";
}
fs::create_directory(dir_path);
std::cout << "Directory created";
} catch (fs::filesystem_error &e) {
std::cerr << e.what();
}
}
Existing directory removed
Directory created
In this example:
fs::exists()
.fs::remove_all()
.fs::create_directory()
.Using fs::remove_all()
ensures that the existing directory and all its contents are deleted, allowing you to create a fresh directory in its place. Always be cautious with this approach, as it will permanently delete all files and subdirectories within the specified directory.
This method provides a clear way to overwrite an existing directory by first removing it and then creating a new one.
Answers to questions are automatically generated and may not have been reviewed.
Create, delete, move, and navigate through directories and files using the standard library's filesystem
module.