C++实现文件夹复制

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012750702/article/details/52738859

语言:C++

环境:Windows10 x64 visual sudio 2013

               Linux Ubuntu16.04 gcc

说明:封装了一个类用于复制文件夹到另一指定目录,

       不删除源文件,

       不改变源文件/文件夹名。

注意:只能复制指定文件夹下的文件,

       无法处理子目录(以后再改),

       windows下要用绝对路径。

成员描述:

1、

void copy(const std::string& srcDirPath, const std::string& desDirPath);

类型:公有。

功能:入口函数,给定源文件夹路径()、和目的文件夹路径,会将源文件夹整个存入目标路径下(而不是只保存内容)。

2、
bool make_dir ( const std::string& pathName);

类型:私有。

功能:用于执行创建文件夹操作,需要提供包含路径的完整文件夹名称

3、
bool get_src_files_name(std::vector<std::string>& fileNameList);

类型:私有。

功能:遍历源文件夹下所有文件名,并存入list中。

4、

void do_copy(const std::vector<std::string>& fileNameList);

类型:私有。

功能:对文件夹下的文件进行拷贝操作,使用到了OpenMP做并行支持,简单使用方法见我另外几篇博文。

      http://blog.csdn.net/u012750702/article/details/51476390

      http://blog.csdn.net/u012750702/article/details/51769576


代码:

copy_dir.h 文件

/**
* author : Ladd7
*/

#ifndef COPYDIR_H
#define COPYDIR_H

#include <string>
#include <vector>

class CopyDir
{
public:
    CopyDir();

    void copy(const std::string& srcDirPath, const std::string& desDirPath);

public:

private:
    bool make_dir (const std::string& pathName);
//    bool mkdir (char const* pathname/*, mode_t mode*/);
    bool get_src_files_name(std::vector<std::string>& fileNameList);
    void do_copy(const std::vector<std::string>& fileNameList);

private:
    std::string srcDirPath, desDirPath;


};

#endif // COPYDIR_H

copy_dir.cpp 文件

/**
* author : Ladd7
*/

#include "copy_dir.h"

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

#if defined(_WIN32)
#   include <direct.h>
#   include <io.h>
#   include <shlobj.h>
#   include <sys/stat.h>
#   include <sys/types.h>
#else // Linux
#   include <dirent.h>
#   include <unistd.h>
#   include <sys/stat.h>
#   include <sys/types.h>
#   include <pwd.h>
#endif

CopyDir::CopyDir()
{

}

void
CopyDir::copy(const std::string& srcDirPath, const std::string& desDirPath)
{
    this->srcDirPath = srcDirPath;
    std::string srcDir;
#ifdef _WIN32
    int n = 0;
    while (srcDirPath.find('\\', n) != std::string::npos)
    {
        n = srcDirPath.find('\\', n) + 1;
    }
    if(n == 0)
    {
        std::cout << "src path error" << std::endl;
        return;
    }
    srcDir = srcDirPath.substr(n-1, srcDirPath.size());

#else  // Linux
    int n = 0;
    while (srcDirPath.find('/', n) != std::string::npos)
    {
        n = srcDirPath.find('/', n) + 1;
    }
    if(n == 0)
    {
        std::cout << "src path error" << std::endl;
        return;
    }
    srcDir = srcDirPath.substr(n-1, srcDirPath.size());

#endif
    this->desDirPath = desDirPath + srcDir;

    if(!make_dir(this->desDirPath))
    {
        return;
    }

    std::vector<std::string> fileNameList;
    if(!get_src_files_name(fileNameList))
    {
        return;
    }

    if(fileNameList.empty())
    {
        std::cout << "src dir is empty" << std::endl;
        return;
    }

    do_copy(fileNameList);
}

bool
CopyDir::make_dir (const std::string& pathName)
{
#ifdef _WIN32
    if(::_mkdir(pathName.c_str()) < 0)
    {
        std::cout << "create path error" << std::endl;
        return false;
    }
#else  // Linux
    if(::mkdir(pathName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP) < 0)
    {
        std::cout << "create path error" << std::endl;
        return false;
    }
#endif

    return true;
}

bool
CopyDir::get_src_files_name(std::vector<std::string>& fileNameList)
{
#ifdef _WIN32
    _finddata_t file;
    long lf;
	std::string src = this->srcDirPath + "\\*.*";
	if ((lf = _findfirst(src.c_str(), &file)) == -1) 
	{
		std::cout << this->srcDirPath << " not found" << std::endl;
		return false;
	}
	else{
		while (_findnext(lf, &file) == 0)
		{
			if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)
				continue;
			fileNameList.push_back(file.name);
		}
	}


    _findclose(lf);
#else  // Linux
    DIR *dir;
    struct dirent *ptr;

    if ((dir=opendir(this->srcDirPath.c_str())) == NULL)
    {
		std::cout << this->srcDirPath << " not found" << std::endl;
        return false;
    }

    while ((ptr=readdir(dir)) != NULL)
    {
        if((ptr->d_name == ".") || (ptr->d_name == ".."))  //current / parent
            continue;
        else if(ptr->d_type == 8)  //file
            fileNameList.push_back(ptr->d_name);
        else if(ptr->d_type == 10)  //link file
            continue;
        else if(ptr->d_type == 4)  //dir
            fileNameList.push_back(ptr->d_name);
    }
    closedir(dir);

#endif

    return true;

}

void
CopyDir::do_copy(const std::vector<std::string> &fileNameList)
{
#pragma omp parallel for
    for (int i = 0; i < fileNameList.size(); i++)
    {
        std::string nowSrcFilePath, nowDesFilePath ;
#ifdef _WIN32
		nowSrcFilePath = this->srcDirPath + "\\" + fileNameList.at(i);
		nowDesFilePath = this->desDirPath + "\\" + fileNameList.at(i);

#else
        nowSrcFilePath = this->srcDirPath + "/" + fileNameList.at(i);
        nowDesFilePath = this->desDirPath + "/" + fileNameList.at(i);

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

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

        out << in.rdbuf();

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


测试:

main.cpp文件

#include <iostream>
#include "copy_dir.h"
using namespace std;

int main(int argc, char *argv[])
{
#ifdef _WIN32
	std::string src = "F:\\data\\test";
	std::string des = "F:\\data\\test2";
	CopyDir cd;
	cd.copy(src, des);
#else
    std::string src = "/media/myUbuntu/F/data/test";
    std::string des = "/media/myUbuntu/F/data/test2";
    CopyDir cd;
    cd.copy(src, des);
#endif
    return 0;
}





猜你喜欢

转载自blog.csdn.net/u012750702/article/details/52738859