filesystem操作

判断文件是否存在/返回文件大小

	std::string rawDataDir = CSystemHelper::GetRawDataDir();
    boost::filesystem::path pathRawDataDir(rawDataDir);
	bool ret = boost::filesystem::exists(pathRawDataDir);

   bool CSystemHelper::IsPathExist(const std::string& sPath)
	{
		try
		{
			boost::filesystem::path path(sPath);
			return boost::filesystem::exists(path);
		}
		catch (...)
		{
			return false;
		}
	}

	std::string CSystemHelper::GetParentPath(const std::string& fileFullPath)
	{
		try
		{
			boost::filesystem::path fullPath = fileFullPath;
			return fullPath.parent_path().string();
		}
		catch (...)
		{
			return "";
		}
	}
	
 uint64_t CSystemHelper::GetFileSize(const std::string& fileFullPath)
	{
		using namespace boost::filesystem;

		uintmax_t size = 0u;
		boost::filesystem::path pathSource(fileFullPath);

		if (exists(pathSource))
		{
			boost::system::error_code error;
			try
			{
				size = file_size(pathSource, error);
			}
			catch(...)
			{
			}
		}

		return static_cast<uint64_t>(size);
	}

(1) 创建文件夹

// 0:failed 1:succeed 2:existed
	int CSystemHelper::CreateDir(const string& dirPath)
	{
		using namespace boost::filesystem;
		if (is_directory(dirPath))
			return 2;
		try
		{
			bool b = create_directories(path(dirPath));
			if(b)
				return 1;
		}
		catch (...)
		{
		}
		return 0;
	}

(2)获取路径下的所有文件

void CSystemHelper::GetAllRawDataFileList(std::vector<std::string>& fileList,const std::string& dir)
	{
		using namespace boost::filesystem;
		if (is_directory(dir))
		{
			directory_iterator end;
			directory_iterator pos(dir);
			for ( pos; pos != end; ++pos)
			{
				if (!is_directory(*pos) && (extension(*pos) == ".raw"))
					fileList.push_back(pos->path().filename().string());
			}
		}	
	}

(3)删除文件

bool CSystemHelper::DeleteFile(const std::string& fullPath)
	{
		using namespace boost::filesystem;
		bool suc = false;
		path  ptest = fullPath;
		if(exists(fullPath))
		{
			try
			{
				suc = remove(ptest);
			}
			catch(...)
			{
				suc = false;
			}

		}
		return suc;
	}

(4)删除整个文件夹

bool CSystemHelper::DeleteDir(const std::string& dir)
	{
		using namespace boost::filesystem;
		bool suc = false;
		path ptest = dir;
		if (exists(ptest))
		{
			try
			{
				if(boost::filesystem::is_empty(ptest))
					suc = remove(ptest);
				else
				{
					uintmax_t num = remove_all(ptest);
					suc = (num != 0);
				}
			}
			catch(...)
			{
				suc = false;
			}
		}
		return suc;
	}

(5)复制文件

	bool CSystemHelper::CopyTo(const std::string& sourfullpath, const std::string& targfullPath, bool overwrite)
	{
		using namespace boost::filesystem;
		path targ = targfullPath;
		path sour = sourfullpath;
		bool suc = false;
		if (exists(sourfullpath) && ((!exists(targfullPath) || overwrite)))
		{
			try
			{
				copy_file(sour, targ,copy_option::overwrite_if_exists);
				suc = true;
			}
			catch(...)
			{
				//
			}
		}	
		return suc;
	}

复制文件夹

bool CSystemHelper::CopyDir(const std::string& sourDir, const std::string& destDir )
	{
		using namespace boost::filesystem;
		boost::filesystem::path PathSource(sourDir) ;
		boost::filesystem::path PathTarget(destDir) ;

		if( !exists(PathSource) )
			return false;

		if( !exists(PathTarget) )
			create_directories(PathTarget);

		directory_iterator end;
		bool suc = true;
		for( directory_iterator it(PathSource); it != end; ++it )
		{
			path newFrom = PathSource;
			newFrom /= it->path().filename().string();

			path newTo = PathTarget;
			newTo /= it->path().filename().string();

			if( is_directory( newFrom ) )
				suc = suc && CopyDir(newFrom.string(), newTo.string() );
			else 
			{
				try
				{
					copy_file(newFrom, newTo,copy_option::overwrite_if_exists);
				}
				catch(...)
				{
					suc = suc && false;
				}
			}
		}
		return suc;
	}

查找文件名相同后缀不同的所有文件

//查找文件名相同后缀不同的所有文件
	//dir:要查询的目录
	//fileName:查询的文件名
	//allFiles: 返回查询结果,带后缀文件名
	void CSystemHelper::find_files_with_name(const string& dir, const string& fileName, vector<string>& allFiles)
	{
		try
		{
			if (!(exists(dir)) || !is_directory(dir))
			{
				return;
			}

			recursive_directory_iterator end;                  
			for (recursive_directory_iterator pos(dir) ;pos != end ; ++pos) //递归目录下的文件
			{
				string fileNameWithExpand = pos->path().filename().string();
				string subFileName = fileNameWithExpand.substr(0u, fileName.length());

				if (subFileName == fileName)
				{
					allFiles.push_back(fileNameWithExpand);
				}
			}
		}
		catch (...)
		{
		}
	}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lz_煜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值