C++复制文件夹
#include "Shellapi.h"#include "tchar.h"#pragma comment(lib,"shell32.lib")
//告訴編譯器在編譯形成的.obj文件和.exe文件中添加一條信息,使得連鏈接器在鏈接庫(kù)時(shí)去直接找shell32.lib這個(gè)庫(kù),不要去找別的庫(kù)在相應(yīng)的函數(shù)中添加如下代碼SHFILEOPSTRUCT fop;fop.wFunc = FO_COPY;//選擇執(zhí)行類型,FO_COPY,FO_DELETE,FO_RENAME,FO_MOVE四種fop.pFrom = _T(“C:\\a\0");//源文件夾的路徑,以"\0"即空為結(jié)尾fop.pTo = _T("C:\\b\0");//拷入文件的文件夾路徑,以"\0"即空為結(jié)尾SHFileOperation(&fop);//現(xiàn)在就已經(jīng)完成COPY功能,但是在COPY過(guò)程中,如果有相同的文件名,則會(huì)出現(xiàn)提示對(duì)話框//如果不想出現(xiàn)提示框,則需要在SHFileOperation(&fop)前添加//fop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR;//這樣就不會(huì)顯示提示窗口了。
// 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());}}
}
?
總結(jié)
- 上一篇: vector、map 内存释放
- 下一篇: C++日志系统log4cxx使用总结