操作系統(tǒng)提供的API通常不支持直接拷貝目錄樹。不過,可以通過遞歸的方法實(shí)現(xiàn)。下面,我們用boost的filesystem庫(kù)實(shí)現(xiàn)該功能。
- // 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());
- }
- }
- }
轉(zhuǎn)自:http://blog.csdn.net/ganxinjiang/article/details/6088323