复制一个指定文件夹下的所有文件,遍历所有子文件夹
只在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;
}