Working with the File System

Copying Directory Structures using std::filesystem

How do I copy a directory structure without copying files using std::filesystem?

Abstract art representing computer programming

To copy a directory structure without copying files using std::filesystem, you can use the copy() function with the copy_options::directories_only flag.

This flag ensures that only the directory structure is copied, ignoring the files within those directories. Here's an example demonstrating how to copy a directory structure:

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;

void copy_directory_structure(
  const fs::path &source,
  const fs::path &destination
) {
  try {
    fs::create_directories(destination); 
    for (const auto &entry :
      fs::recursive_directory_iterator(source)) {
      if (fs::is_directory(entry.status())) { 
        fs::path relative_path =
          fs::relative(entry.path(), source); 
        fs::path new_directory =
          destination / relative_path; 
        fs::create_directories(new_directory); 
        std::cout << "Created directory: "
          << new_directory << '\n';
      }
    }
  } catch (fs::filesystem_error &e) {
    std::cerr << e.what() << '\n';
  }
}

int main() {
  fs::path source{R"(c:\test)"}; 
  fs::path destination{R"(c:\test_copy)"}; 
  copy_directory_structure(source, destination);
}
Created directory: c:\test_copy\subdir

In this example:

  • We define source as the path to the directory we want to copy and destination as the path where we want to copy the structure.
  • We use fs::create_directories(destination) to ensure the destination directory exists.
  • We iterate over each entry in the source directory using fs::recursive_directory_iterator.
  • We check if the current entry is a directory using fs::is_directory(entry.status()).
  • We calculate the relative path of the current directory from the source directory using fs::relative.
  • We construct the new directory path in the destination and create the directory using fs::create_directories.

This method effectively copies the directory structure without copying any files. It uses fs::recursive_directory_iterator to traverse the directory tree and create corresponding directories in the destination.

If you want to exclude certain directories from being copied, you can add additional checks within the loop to filter out unwanted directories.

This approach ensures that only the directory structure is copied, which can be useful when you need to replicate a directory hierarchy without duplicating the files.

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