filesystem判断文件或文件夹是否存在

class CFileSystem {
public:
	CFileSystem(CFileSystem&) = delete;
	CFileSystem(CFileSystem&&) = delete;
	CFileSystem(){}
	/// <summary>
	/// 判断文件或文件夹是否存在
	/// </summary>
	/// <param name="path">路径</param>
	/// <param name="is_directory">true:判断是否是文件夹,false:判断是否是文件</param>
	/// <returns>路径的文件或文件夹是否存在</returns>
	bool IsExist(const std::filesystem::path& path, bool is_directory)const {

		std::error_code error;
		auto file_status = std::filesystem::status(path, error);
		if (error) {
			return false;
		}

		if (!std::filesystem::exists(file_status)) {
			return false;
		}
		return  std::filesystem::is_directory(file_status) && is_directory;
	}
	bool IsFileExist(const std::filesystem::path& path)const {
		return IsExist(path, false);
	}
	bool IsFileExist(const std::string_view& path)const {
		return IsFileExist(std::filesystem::path(path));
	}
	bool IsFileExist(const std::wstring_view& path)const {
		return IsFileExist(std::filesystem::path(path));
	}
	bool IsFileExist(const wchar_t* path)const {
		return IsFileExist(std::wstring_view(path));
	}
	bool IsFileExist(const char* path)const {
		return IsFileExist(std::string_view(path));
	}
	bool IsDirExist(const std::filesystem::path& path)const {
		return IsExist(path, true);
	}
	bool IsDirExist(const std::string_view& path)const {
		return IsDirExist(std::filesystem::path(path));
	}
	bool IsDirExist(const std::wstring_view& path)const {
		return IsDirExist(std::filesystem::path(path));
	}
	bool IsDirExist(const wchar_t* path)const {
		return IsDirExist(std::wstring_view(path));
	}
	bool IsDirExist(const char* path)const {
		return IsDirExist(std::string_view(path));
	}
	
	std::filesystem::path::string_type GetCurrentPath()const
	{
		using std::filesystem::current_path;
		return current_path().native();
	}
	void Emun(const std::filesystem::path& path,bool EnumChild,std::function<bool(const std::filesystem::path&)> callback)const
	{
		using std::filesystem::directory_iterator;
		for (auto& p : directory_iterator(path)) {
			if (!callback(p.path()))
				break;
			if (EnumChild && IsExist(p.path(),true))
			{
				Emun(p.path(), EnumChild, callback);
			}
		}
	}
};

bool PrintDir(const std::filesystem::path& path)
{
	std::cout << path<<std::endl;
	return true;
}

int main()
{
	CFileSystem filesystem;
	std::cout << "-----------------------------filesystem test---------------------------------------" << std::endl;
	std::cout << "" << std::endl;

	std::cout << "-----------------------------GetCurrentPath---------------------------------------" << std::endl;

	std::wcout << filesystem.GetCurrentPath()<<std::endl;
	std::cout << "-------------------------------Emun------------------------------------------------" << std::endl;

	filesystem.Emun(filesystem.GetCurrentPath(),true, std::bind_front(PrintDir));
	std::cout << "------------------------------------------------------------------------------------" << std::endl;
	filesystem.Emun(filesystem.GetCurrentPath(), false, std::bind_front(PrintDir));

	std::cout << "--------------------------------IsDirExist-----------------------------------------------" << std::endl;

	std::cout <<std::boolalpha << filesystem.IsDirExist(LR"(.)") << std::endl;
	std::cout << std::boolalpha << filesystem.IsDirExist(".") << std::endl;
	std::cout << "--------------------------------IsFileExist-----------------------------------------------" << std::endl;

	std::cout << std::boolalpha << filesystem.IsFileExist(LR"(.)") << std::endl;
	std::cout << std::boolalpha << filesystem.IsFileExist(".") << std::endl;

	std::cout << std::boolalpha << filesystem.IsFileExist(LR"(.\CFileSystem.exe)") << std::endl;
	std::cout << std::boolalpha << filesystem.IsFileExist(R"(..\Debug\CFileSystem.exe)") << std::endl;

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值