Working with the File System

Overwriting an Existing Directory

What happens if I try to create a directory that already exists?

Abstract art representing computer programming

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:

  • We define dir_path as the path to the directory we want to create.
  • We call fs::create_directory() and check its return value.
  • If the directory already exists, 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.
  • If the directory structure already exists, it simply returns 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.

Overwriting an Existing Directory

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:

  • We check if the directory exists using fs::exists().
  • If it exists, we remove it and all its contents using fs::remove_all().
  • After removing the existing directory, we create a new one using 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.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved