كيف يمكنني نسخ الملفات والمجلدات باستخدام Boost and Visual Studio 2005؟

StackOverflow https://stackoverflow.com/questions/1853812

سؤال

أحاول استخدام Bost :: نظام الملفات لنسخ الملفات والمجلدات (تماما مثل نسخ معيار معجون ولصقه في مستكشف Windows).

على الرغم من أنني كنت إلى دفعة :: وثائق النظام, ، ما زلت لا أعرف حقا كيف أذهب في القيام بذلك.

هل يجب أن تذهب بشكل متكرر على الرغم من أن كل دليل (إنشاءه) وابحث عن كل ملف نسخه؟

بالإضافة إلى ذلك، كيف يمكنك نسخ الملف في C ++ / BOOST؟

PS أنا أستخدم دفعة 1.40.0

تحديثأعتقد أنني ربما انتهى الأمر بإنشاء إجابة على هذا واحد، فقط القلق الذي لا أقوم به بأي أخطاء في المحاولة للتحقق من الملفات والمجلدات المقفلة.

يتعين على التعليمات البرمجية التالية نسخة من دليل في المسار النسبي "../example/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...
}

سؤالي الآن هو ما أؤيد القيام به فيما يتعلق بالملفات والأدلة المقفلة؟

هل كانت مفيدة؟

المحلول

زيادة :: نظام الملفات :: refursive_directory_iterator

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top