C++实现文件夹复制

来源:互联网 发布:射精后强制刺激 知乎 编辑:程序博客网 时间:2024/06/15 01:34

标签: C++
 1396人阅读 评论(1) 收藏 举报
 分类:

语言:C++

环境:Windows10 x64 visual sudio 2013

               Linux Ubuntu16.04 gcc

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

       不删除源文件,

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

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

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

       windows下要用绝对路径。

成员描述:

1、

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void copy(const std::string& srcDirPath, const std::string& desDirPath);  

类型:公有。

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

2、
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. bool make_dir ( const std::string& pathName);  

类型:私有。

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

3、
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. bool get_src_files_name(std::vector<std::string>& fileNameList);  

类型:私有。

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

4、

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 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 文件

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2. * author : Ladd7 
  3. */  
  4.   
  5. #ifndef COPYDIR_H  
  6. #define COPYDIR_H  
  7.   
  8. #include <string>  
  9. #include <vector>  
  10.   
  11. class CopyDir  
  12. {  
  13. public:  
  14.     CopyDir();  
  15.   
  16.     void copy(const std::string& srcDirPath, const std::string& desDirPath);  
  17.   
  18. public:  
  19.   
  20. private:  
  21.     bool make_dir (const std::string& pathName);  
  22. //    bool mkdir (char const* pathname/*, mode_t mode*/);  
  23.     bool get_src_files_name(std::vector<std::string>& fileNameList);  
  24.     void do_copy(const std::vector<std::string>& fileNameList);  
  25.   
  26. private:  
  27.     std::string srcDirPath, desDirPath;  
  28.   
  29.   
  30. };  
  31.   
  32. #endif // COPYDIR_H  

copy_dir.cpp 文件

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2. * author : Ladd7 
  3. */  
  4.   
  5. #include "copy_dir.h"  
  6.   
  7. #include <iostream>  
  8. #include <fstream>  
  9. #include <cstring>  
  10.   
  11. #if defined(_WIN32)  
  12. #   include <direct.h>  
  13. #   include <io.h>  
  14. #   include <shlobj.h>  
  15. #   include <sys/stat.h>  
  16. #   include <sys/types.h>  
  17. #else // Linux  
  18. #   include <dirent.h>  
  19. #   include <unistd.h>  
  20. #   include <sys/stat.h>  
  21. #   include <sys/types.h>  
  22. #   include <pwd.h>  
  23. #endif  
  24.   
  25. CopyDir::CopyDir()  
  26. {  
  27.   
  28. }  
  29.   
  30. void  
  31. CopyDir::copy(const std::string& srcDirPath, const std::string& desDirPath)  
  32. {  
  33.     this->srcDirPath = srcDirPath;  
  34.     std::string srcDir;  
  35. #ifdef _WIN32  
  36.     int n = 0;  
  37.     while (srcDirPath.find('\\', n) != std::string::npos)  
  38.     {  
  39.         n = srcDirPath.find('\\', n) + 1;  
  40.     }  
  41.     if(n == 0)  
  42.     {  
  43.         std::cout << "src path error" << std::endl;  
  44.         return;  
  45.     }  
  46.     srcDir = srcDirPath.substr(n-1, srcDirPath.size());  
  47.   
  48. #else  // Linux  
  49.     int n = 0;  
  50.     while (srcDirPath.find('/', n) != std::string::npos)  
  51.     {  
  52.         n = srcDirPath.find('/', n) + 1;  
  53.     }  
  54.     if(n == 0)  
  55.     {  
  56.         std::cout << "src path error" << std::endl;  
  57.         return;  
  58.     }  
  59.     srcDir = srcDirPath.substr(n-1, srcDirPath.size());  
  60.   
  61. #endif  
  62.     this->desDirPath = desDirPath + srcDir;  
  63.   
  64.     if(!make_dir(this->desDirPath))  
  65.     {  
  66.         return;  
  67.     }  
  68.   
  69.     std::vector<std::string> fileNameList;  
  70.     if(!get_src_files_name(fileNameList))  
  71.     {  
  72.         return;  
  73.     }  
  74.   
  75.     if(fileNameList.empty())  
  76.     {  
  77.         std::cout << "src dir is empty" << std::endl;  
  78.         return;  
  79.     }  
  80.   
  81.     do_copy(fileNameList);  
  82. }  
  83.   
  84. bool  
  85. CopyDir::make_dir (const std::string& pathName)  
  86. {  
  87. #ifdef _WIN32  
  88.     if(::_mkdir(pathName.c_str()) < 0)  
  89.     {  
  90.         std::cout << "create path error" << std::endl;  
  91.         return false;  
  92.     }  
  93. #else  // Linux  
  94.     if(::mkdir(pathName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP) < 0)  
  95.     {  
  96.         std::cout << "create path error" << std::endl;  
  97.         return false;  
  98.     }  
  99. #endif  
  100.   
  101.     return true;  
  102. }  
  103.   
  104. bool  
  105. CopyDir::get_src_files_name(std::vector<std::string>& fileNameList)  
  106. {  
  107. #ifdef _WIN32  
  108.     _finddata_t file;  
  109.     long lf;  
  110.     std::string src = this->srcDirPath + "\\*.*";  
  111.     if ((lf = _findfirst(src.c_str(), &file)) == -1)   
  112.     {  
  113.         std::cout << this->srcDirPath << " not found" << std::endl;  
  114.         return false;  
  115.     }  
  116.     else{  
  117.         while (_findnext(lf, &file) == 0)  
  118.         {  
  119.             if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)  
  120.                 continue;  
  121.             fileNameList.push_back(file.name);  
  122.         }  
  123.     }  
  124.   
  125.   
  126.     _findclose(lf);  
  127. #else  // Linux  
  128.     DIR *dir;  
  129.     struct dirent *ptr;  
  130.   
  131.     if ((dir=opendir(this->srcDirPath.c_str())) == NULL)  
  132.     {  
  133.         std::cout << this->srcDirPath << " not found" << std::endl;  
  134.         return false;  
  135.     }  
  136.   
  137.     while ((ptr=readdir(dir)) != NULL)  
  138.     {  
  139.         if((ptr->d_name == ".") || (ptr->d_name == ".."))  //current / parent  
  140.             continue;  
  141.         else if(ptr->d_type == 8)  //file  
  142.             fileNameList.push_back(ptr->d_name);  
  143.         else if(ptr->d_type == 10)  //link file  
  144.             continue;  
  145.         else if(ptr->d_type == 4)  //dir  
  146.             fileNameList.push_back(ptr->d_name);  
  147.     }  
  148.     closedir(dir);  
  149.   
  150. #endif  
  151.   
  152.     return true;  
  153.   
  154. }  
  155.   
  156. void  
  157. CopyDir::do_copy(const std::vector<std::string> &fileNameList)  
  158. {  
  159. #pragma omp parallel for  
  160.     for (int i = 0; i < fileNameList.size(); i++)  
  161.     {  
  162.         std::string nowSrcFilePath, nowDesFilePath ;  
  163. #ifdef _WIN32  
  164.         nowSrcFilePath = this->srcDirPath + "\\" + fileNameList.at(i);  
  165.         nowDesFilePath = this->desDirPath + "\\" + fileNameList.at(i);  
  166.   
  167. #else  
  168.         nowSrcFilePath = this->srcDirPath + "/" + fileNameList.at(i);  
  169.         nowDesFilePath = this->desDirPath + "/" + fileNameList.at(i);  
  170.   
  171. #endif  
  172.         std::ifstream in;  
  173.         in.open(nowSrcFilePath);  
  174.         if(!in)  
  175.         {  
  176.             std::cout << "open src file : " << nowSrcFilePath << " failed" << std::endl;  
  177.             continue;  
  178.         }  
  179.   
  180.         std::ofstream out;  
  181.         out.open(nowDesFilePath);  
  182.         if(!out)  
  183.         {  
  184.             std::cout << "create new file : " << nowDesFilePath << " failed" << std::endl;  
  185.             in.close();  
  186.             continue;  
  187.         }  
  188.   
  189.         out << in.rdbuf();  
  190.   
  191.         out.close();  
  192.         in.close();  
  193.     }  
  194. }  


测试:

main.cpp文件

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include "copy_dir.h"  
  3. using namespace std;  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7. #ifdef _WIN32  
  8.     std::string src = "F:\\data\\test";  
  9.     std::string des = "F:\\data\\test2";  
  10.     CopyDir cd;  
  11.     cd.copy(src, des);  
  12. #else  
  13.     std::string src = "/media/myUbuntu/F/data/test";  
  14.     std::string des = "/media/myUbuntu/F/data/test2";  
  15.     CopyDir cd;  
  16.     cd.copy(src, des);  
  17. #endif  
  18.     return 0;  
  19. }  
0 0