boost 复制文件夹

#include <iostream>

#include <boost/filesystem.hpp>
#include <boost/progress.hpp>
#include <boost/foreach.hpp>


void find_files(const boost::filesystem::path& dir,const std::string& filename,std::vector<boost::filesystem::path>& v)
{

	if (!boost::filesystem::exists(dir) || !boost::filesystem::is_directory(dir)) return;

	boost::filesystem::recursive_directory_iterator end;

	for (boost::filesystem::recursive_directory_iterator pos(dir);pos!=end;++pos)
	{
		if (!boost::filesystem::is_directory(*pos)/* && pos->path().filename().string() == filename*/)
		{
			v.push_back(pos->path());
		}
	}

}

size_t copy_files(const boost::filesystem::path& from_dir,const boost::filesystem::path& to_dir,const std::string& filename = "*")
{
	if (!boost::filesystem::is_directory(from_dir))
	{
		std::cout <<"args is not a dir." << std::endl;
		return 0;
	}

	std::cout << "prepare for copy,please wait..."<< std::endl;

	std::vector<boost::filesystem::path> v;

	find_files(from_dir,filename,v);

	if (v.empty())
	{
		std::cout << "0 file copied." << std::endl;

		return 0;
	}

	std::cout << "now begin copy files ..." << std::endl;

	boost::filesystem::path tmp;

	boost::progress_display pd(v.size());

	BOOST_FOREACH(boost::filesystem::path &p,v)
	{
		tmp = to_dir / p.string().substr(from_dir.string().length());

		if (!boost::filesystem::exists(tmp.parent_path()))
		{
			boost::filesystem::create_directories(tmp.parent_path());
		}

		boost::filesystem::copy_file(p,tmp);

		++pd;
	}

	std::cout << v.size() << "file copied." << std::endl;

	return v.size();
}

int main(int argc, char **argv)
{
	copy_files("I:\\QT","I:\\TQ");
}

好的,我会尽力回答你的问题。对于你的问题,我可以提供如下的解决方案: 首先,需要使用C++11中的文件和目录操作库来实现文件夹移动。推荐使用Boost库或者C++17中的filesystem库。 针对你的要求,我们可以实现一个FolderMover类,包含以下成员函数: 1. moveFolder(string sourcePath, string destPath):移动文件夹函数,接受源文件夹路径和目标文件夹路径作为参数,返回移动是否成功。在函数内部,根据源文件夹和目标文件夹所在分区判断使用剪切或者拷贝方式进行移动,使用多线程进行拷贝操作,并且可以中断操作。移动完成后,删除原文件夹中成功进行移动的文件,如果全部成功完成移动则删除整个文件夹。 2. traverseFolder(string folderPath):遍历文件夹函数,接受文件夹路径作为参数,返回该文件夹下所有文件的路径列表。如果该文件夹是一个空文件夹则返回空列表。 3. copyFolder(string sourcePath, string destPath):拷贝文件夹函数,接受源文件夹路径和目标文件夹路径作为参数,返回拷贝是否成功。在函数内部,使用多线程进行拷贝操作,并且可以中断操作。 4. deleteFolder(string folderPath):删除文件夹函数,接受文件夹路径作为参数,返回删除是否成功。在函数内部,递归删除该文件夹下所有文件和文件夹。 5. calculateTime():统计文件夹移动所使用的时间。 在moveFolder函数中,我们可以使用Boost库或者C++17中的filesystem库中的exists函数来判断源文件夹和目标文件夹是否存在,使用is_directory函数来判断是否为文件夹,使用copy函数或者copy_file函数来进行文件或者文件夹的拷贝,使用remove函数来删除文件或者文件夹,使用time函数来获取系统时间。 在moveFolder函数中,我们可以使用递归方式来遍历文件夹,使用多线程来进行文件夹的拷贝操作。当拷贝操作失败时,我们可以使用遍历文件夹的方式一个个的文件进行移动。 在moveFolder函数中,我们可以使用std::condition_variable和std::mutex来实现操作的中断。 这里提供一个简单的代码框架,具体实现需要根据具体的需求进行调整和完善: ```c++ #include <iostream> #include <string> #include <vector> #include <chrono> #include <thread> #include <mutex> #include <condition_variable> #include <boost/filesystem.hpp> using namespace std; using namespace boost::filesystem; class FolderMover { public: bool moveFolder(string sourcePath, string destPath) { // TODO: 判断源文件夹和目标文件夹是否存在 // TODO: 判断源文件夹和目标文件夹是否在同一分区 // TODO: 使用剪切或者拷贝方式进行移动 // TODO: 使用多线程进行拷贝操作 // TODO: 可以中断操作 // TODO: 删除原文件夹中成功进行移动的文件 // TODO: 如果全部成功完成移动则删除整个文件夹 return true; } vector<string> traverseFolder(string folderPath) { // TODO: 遍历文件夹,返回该文件夹下所有文件的路径列表 // TODO: 如果该文件夹是一个空文件夹则返回空列表 vector<string> fileList; return fileList; } bool copyFolder(string sourcePath, string destPath) { // TODO: 拷贝文件夹,返回拷贝是否成功 // TODO: 使用多线程进行拷贝操作 // TODO: 可以中断操作 return true; } bool deleteFolder(string folderPath) { // TODO: 删除文件夹,返回删除是否成功 // TODO: 递归删除该文件夹下所有文件和文件夹 return true; } double calculateTime() { // TODO: 统计文件夹移动所使用的时间 double time = 0.0; return time; } private: // TODO: 定义需要的成员变量 }; int main() { FolderMover mover; string sourcePath = "/source/path"; string destPath = "/dest/path"; auto start = chrono::system_clock::now(); bool result = mover.moveFolder(sourcePath, destPath); auto end = chrono::system_clock::now(); double elapsed_seconds = chrono::duration_cast<chrono::duration<double>>(end - start).count(); if (result) { cout << "Folder moved successfully in " << elapsed_seconds << " seconds." << endl; } else { cout << "Failed to move folder." << endl; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值