C++复制文件夹

4 篇文章 0 订阅
#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());
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值