문제

Boost :: FileSystem을 사용하여 파일과 폴더를 복사하려고합니다 (표준 복사 폴더와 마찬가지로 Windows Explorer에 붙여 넣기).

나는 그에게 갔지만 부스트 :: 파일 시스템 문서, 나는 아직도이 작업을 수행하는 방법을 정말로 모른다.

각 디렉토리 (생성)를 통해 재귀 적으로 가서 각 파일을 복사해야합니까?

또한 파일을 C ++/BOST로 어떻게 복사합니까?

추신 : 부스트 1.40.0을 사용하고 있습니다

업데이트나는 이것에 대한 답을 만들었을 수도 있다고 생각합니다. 잠긴 파일과 폴더를 확인하기 위해 시도 캐치 오류를하지 않는다는 것이 걱정됩니다.

다음 코드는 상대 경로에서 디렉토리의 사본을 만들어 "../example/ecomm"을 기존 경로 "../example/dup_ecomm"으로 복제합니다.

#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/regex.hpp>
#include <iostream>
#include <fstream>
#include<string>

bool copy_dir( const boost::filesystem::path & ext_dir_path,         // the existing directory
               const boost::filesystem::path & duplicate_dir_path    // the duplicate directory
             )
{
  std::cout << "BEGIN: copy_dir " << endl;
  std::cout << "- ext_dir_path: " << ext_dir_path << endl;
  std::cout << "- duplicate_dir_path: " << duplicate_dir_path << endl;

  // 1. Ensure that the directory we are trying to copy exists.
  if (!boost::filesystem::exists( ext_dir_path ) ) return false;

  bool createdDir = boost::filesystem::create_directory( duplicate_dir_path );

  // cout << "createdDir: " << createdDir << endl;


  copy_dir(ext_dir_path,         // the existing directory
           duplicate_dir_path,   // the duplicate directory,
           ext_dir_path,    // the base path for the existing directory
           duplicate_dir_path,
           true);

  std::cout << "END: copy_dir " << endl;
}


bool copy_dir( const boost::filesystem::path & ext_dir_path,         // the existing directory
               const boost::filesystem::path & duplicate_dir_path,   // the duplicate directory,
               const boost::filesystem::path & base_ext_dir_path,    // the base path for the existing directory
               const boost::filesystem::path & base_duplicate_dir_path, // the base path for the duplicate of the exisiting directory
               bool isRootPath)
{
  // Debug input arguments
  std::cout << "BEGIN: copy_dir " << endl;
  std::cout << "- ext_dir_path: " << ext_dir_path << endl;
  std::cout << "- duplicate_dir_path: " << duplicate_dir_path << endl;
  std::cout << "- base_ext_dir_path: " << base_ext_dir_path << endl;
  std::cout << "- base_duplicate_dir_path: " << base_duplicate_dir_path << endl;
  std::cout << "- isRootPath: " << isRootPath << endl;

  boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end

  cout << "--Beginning itr loop" << endl;
  for ( boost::filesystem::directory_iterator itr( ext_dir_path );
        itr != end_itr;
        ++itr )
  {

    if ( boost::filesystem::is_directory(itr->status()) )
    {
     cout << "---itr->path(): " << itr->path() << endl;


     boost::filesystem::path newExtDir(itr->path());

     string dup_path = itr->path().string();
     boost::algorithm::replace_first(dup_path, base_ext_dir_path.string(), base_duplicate_dir_path.string());
     cout << "dup_path: " << dup_path << endl;

     boost::filesystem::path new_dup_dir(dup_path);

     bool createdDir = boost::filesystem::create_directory( new_dup_dir );

     cout << "creating directory " << dup_path << " created: " << createdDir << endl;

     boost::filesystem::path newDuplicateDir(duplicate_dir_path);

     copy_dir(newExtDir,         // the existing directory
          newDuplicateDir,   // the duplicate directory,
          base_ext_dir_path,
          base_duplicate_dir_path,
          false);
    }
    else 
    {
            cout << "---isLeaf: " << itr->path() << endl;

        string dup_path = itr->path().string();

        boost::algorithm::replace_first(dup_path, base_ext_dir_path.string(), base_duplicate_dir_path.string());

        string src_path = itr->path().string();

        cout << "src_path: " << src_path << endl;
        cout << "dup_path: " << dup_path << endl;

        boost::filesystem::path s_path(src_path);
        boost::filesystem::path d_path(dup_path);

        boost::filesystem::copy_file(s_path, d_path);
    }

  }
  std::cout << "--Ending itr loop" << endl;


  std::cout << "END: copy_dir " << endl;

  return false;
}

test_suite*
init_unit_test_suite( int, char* [] ) {

    boost::filesystem::path ext_dir("..\\example\\ecomm");
    boost::filesystem::path dir_dup("..\\example\\dup_ecomm");

    copy_dir(ext_dir,
             dir_dup);   // the duplicate directory,

        // ... unit tests...etc...
}

내 질문은 잠긴 파일과 디렉토리와 관련하여 내가 잊고있는 일입니까?

도움이 되었습니까?

해결책

부스트 :: FileSystem :: recursive_directory_iterator

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top