C++ 实现文件夹复制

本文介绍了一种在Linux环境下复制文件夹及其所有子文件夹和文件的方法,使用C++编程语言实现。代码中详细展示了如何遍历源目录,创建目标目录结构,并复制文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

复制一个指定文件夹下的所有文件,遍历所有子文件夹

只在linux下实现,如果需要windows下实现,可以留言

copyDir.hpp

#include <string>
#include <vector>

#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>

#include <iostream>
#include <fstream>
#include <cstring>


class CopyDir
{
public:
    CopyDir();
    ~CopyDir();

    bool doCopy(std::string& src_path,std::string& des_path);
private:
    bool readDir(std::string path);
    std::vector<std::string> file_list;
    std::vector<std::string> dir_list;

    std::string src_path_,
                des_path_;
    int src_n_;
};

CopyDir::CopyDir()
{

}
CopyDir::~CopyDir()
{

}


bool CopyDir::readDir(std::string path)
{
    char* c_path=const_cast<char*>(path.c_str());
    DIR *dir;
    struct dirent *ptr;

    if ((dir=opendir(c_path)) == NULL)
    {
    std::cout << path << " not found" << std::endl;
        return false;
    }

    while ((ptr=readdir(dir)) != NULL)
    {
        if((strcmp(ptr->d_name,".")==0) || strcmp(ptr->d_name,"..") ==0)  //current / parent
            continue;
        else if(ptr->d_type == 8)  //file
    {
        std::string son_file=path+"/"+ptr->d_name;
        file_list.push_back(son_file);
    }

        else if(ptr->d_type == 10)  //link file
            continue;
        else if(ptr->d_type == 4)   //dir
    {
        std::string son_path=path+"/"+ptr->d_name;
        readDir(son_path);
        dir_list.push_back(son_path);
    }
    }
    closedir(dir);

    return true;
}

bool CopyDir::doCopy(std::string& src_path, std::string& des_path)
{
    src_path_=src_path;
    des_path_=des_path;
    char* c_src_path=const_cast<char*>(src_path.c_str());
    char* c_des_path=const_cast<char*>(des_path.c_str());

    std::string son_src_dir;

    int from_n = 0;
    while (src_path.find('/', from_n) != std::string::npos)
    {
        from_n = src_path.find('/', from_n) + 1;
    }
    if(from_n == 0)
    {
        std::cout << "src path error" << std::endl;
        return false;
    }
    src_n_=from_n;
    son_src_dir = src_path.substr(from_n-1, src_path.size());


    des_path_+=son_src_dir;

    if(::mkdir(des_path_.c_str(), S_IRWXU | S_IRGRP | S_IXGRP) < 0)
    {
        std::cout << "create path error" << std::endl;
        return false;
    }

    readDir(src_path);


    if(!dir_list.empty())
    {
        for(int i=dir_list.size()-1;i>=0;i--)
        {
            std::string now_dir=des_path_+dir_list[i].substr(src_path_.size(),dir_list[i].size());
            if(::mkdir(now_dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP) < 0)
            {
                std::cout << "create path error" << std::endl;
                return false;
            }
        }
    }
    if(!file_list.empty())
    {
    for (int i = 0; i < file_list.size(); i++)
    {
        std::string now_src_path, now_des_path ;
        now_src_path =file_list.at(i);
        int now_n = 0;
        while (now_src_path.find('/', now_n) != std::string::npos)
        {
            now_n = now_src_path.find('/', now_n) + 1;
        }
        if(now_n == 0)
        {
            std::cout << "src path error" << std::endl;
            return false;
        }
        now_des_path=des_path_+now_src_path.substr(src_path_.size(),now_src_path.size());

        std::ifstream in;
        in.open(now_src_path);
        if(!in)
        {
            std::cout << "open src file : " << now_src_path << " failed" << std::endl;
            continue;
        }

        std::ofstream out;
        out.open(now_des_path);
        if(!out)
        {
            std::cout << "create new file : " << now_des_path << " failed" << std::endl;
            in.close();
            continue;
        }

        out << in.rdbuf();

        out.close();
        in.close();
    }
    }

    return true;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(testCopy LANGUAGES CXX)

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(testCopy main.cpp)

测试
main.cpp

#include "copyDir.hpp"

int main()
{
    std::string path_from="/home/ecci/testCopyFrom";
        std::string path_to="/home/ecci/testCopyTo";
    CopyDir cpd;
    cpd.doCopy(path_from,path_to);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值