欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

C++复制文件夹

发布时间:2025/3/21 c/c++ 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++复制文件夹 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
#include "Shellapi.h"#include "tchar.h"#pragma comment(lib,"shell32.lib") //告诉编译器在编译形成的.obj文件和.exe文件中添加一条信息,使得连链接器在链接库时去直接找shell32.lib这个库,不要去找别的库在相应的函数中添加如下代码SHFILEOPSTRUCT fop;fop.wFunc = FO_COPY;//选择执行类型,FO_COPY,FO_DELETE,FO_RENAME,FO_MOVE四种fop.pFrom = _T(“C:\\a\0");//源文件夹的路径,以"\0"即空为结尾fop.pTo = _T("C:\\b\0");//拷入文件的文件夹路径,以"\0"即空为结尾SHFileOperation(&fop);//现在就已经完成COPY功能,但是在COPY过程中,如果有相同的文件名,则会出现提示对话框//如果不想出现提示框,则需要在SHFileOperation(&fop)前添加//fop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR;//这样就不会显示提示窗口了。 // recusively copy file or directory from $src to $dst void CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst) {if (! boost::filesystem::exists(dst)){boost::filesystem::create_directories(dst);}for (boost::filesystem::directory_iterator it(src); it != boost::filesystem::directory_iterator(); ++it){const boost::filesystem::path newSrc = src / it->filename();const boost::filesystem::path newDst = dst / it->filename();if (boost::filesystem::is_directory(newSrc)){CopyFiles(newSrc, newDst);}else if (boost::filesystem::is_regular_file(newSrc)){boost::filesystem::copy_file(newSrc, newDst, boost::filesystem::copy_option::overwrite_if_exists);}else{_ftprintf(stderr, "Error: unrecognized file - %s", newSrc.string().c_str());}} }

 

总结

以上是生活随笔为你收集整理的C++复制文件夹的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。