C++17 std::filesystem 高阶用法

写在最前:std::filesystem 需要C++17或以上的支持,如果你也是使用visual studio开发,那么可以通过 项目属性 > 配置属性 > C/C++ > 语言 > C++语言标准 进行设置。
在这里插入图片描述
本文将针对以下场景,对filesystem进行demo验证:

  • 遍历当前目录下所有文件与文件夹
std::vector<std::string> TraversingFiles(std::string dirName = "./")
{
	namespace fs = std::filesystem;
	std::vector<std::string> files = {};
	fs::path url(dirName);
	if (!fs::exists(url))
	{
		return files;
	}

	fs::directory_entry input(url);
	if (input.status().type() != fs::file_type::directory)
	{
		return files;
	}

	fs::directory_iterator dir(url);
	for (auto v : dir)
	{
		files.emplace_back(v.path().string());
		//cout << v.path().filename() << endl;
	}
	return files;
}
  • 遍历当前目录中所有文件(只限文件)
std::vector<std::string> TraversingFilesOnly(std::string dirName = "./")
{
	namespace fs = std::filesystem;
	std::vector<std::string> files = {};
	fs::path url(dirName);
	if (!fs::exists(url))
	{
		return files;
	}

	fs::directory_iterator iter(url);
	for (fs::directory_iterator end; iter != end; ++iter)
	{
		if (!fs::is_directory(iter->path()))
		{
			//files.emplace_back(begin->path().filename().string());
			files.emplace_back(iter->path().string());
		}
	}
	return files;
}
  • 递归遍历目录下所有的文件
std::vector<std::string> TraversingFilesRecursive(std::string dirName = "./")
{
	namespace fs = std::filesystem;
	std::vector<std::string> files = {};
	fs::path url(dirName);
	if (!fs::exists(url))
	{
		return files;
	}

	fs::recursive_directory_iterator begin(url);
	for (fs::recursive_directory_iterator end; begin != end; ++begin)
	{
		/*if (fs::is_regular_file(begin->path().string()))
		{
			files.emplace_back(begin->path().string());
		}*/

		if (!fs::is_directory(begin->path()))
		{
			files.emplace_back(begin->path().string());
		}
	}
	return files;
}
  • 删除当前目录下所有的文件(不含文件夹)
bool DeleteFiles(std::string dirName = "./")
{
	namespace fs = std::filesystem;
	fs::path root(dirName);
	std::vector<std::string> files = {};
	for (fs::directory_iterator end, itr(root); itr != end; ++itr)
	{
		if (!fs::is_directory(itr->path()))
		{
			files.emplace_back(itr->path().string());
		}
	}

	uintmax_t count = 0;
	for (auto v : files)
	{
		uintmax_t cnt = fs::remove_all(v);
		count += cnt;
	}
	return (count == files.size()) ? true : false;
}
  • 递归删除目录下所有的文件
bool DeleteFilesRecursive(std::string dirName = "./")
{
	namespace fs = std::filesystem;
	fs::path root(dirName);
	std::vector<std::string> files = {};
	fs::recursive_directory_iterator begin(root);
	for (fs::recursive_directory_iterator end; begin != end; ++begin)
	{
		if (!fs::is_directory(begin->path()))
		{
			files.emplace_back(begin->path().string());
		}
	}

	uintmax_t count = 0;
	for (auto v : files)
	{
		uintmax_t cnt = fs::remove_all(v);
		count += cnt;
	}
	return (count == files.size()) ? true : false;
}

测试代码:

#include <iostream>
#include <filesystem>
#include <string>
#include <vector>

int main()
{
	namespace fs = std::filesystem;
	std::cout << "fs demo:\n" << std::boolalpha;

	std::cout << "------------------\n";
	std::string dirName = fs::current_path().string();
	std::vector<std::string> files = TraversingFiles(dirName);
	for (auto &v : files)
	{
		std::cout << v.c_str() << std::endl;
	}

	std::cout << "------------------\n";
	files = TraversingFilesOnly(dirName);
	for (auto& v : files)
	{
		std::cout << v.c_str() << std::endl;
	}

	std::cout << "------------------\n";
	files = TraversingFilesRecursive(dirName);
	for (auto& v : files)
	{
		std::cout << v.c_str() << std::endl;
	}

	std::cout << "------------------\n";
	dirName = "C:\\Users\\Kandy\\Desktop\\toDelete";
	bool okey = DeleteFiles(dirName);
	std::cout << "okey=" << okey;


	std::cout << "------------------\n";
	dirName = "C:\\Users\\Kandy\\Desktop\\toDelete2";
	okey = DeleteFilesRecursive(dirName);
	std::cout << "okey=" << okey;
	
	return 0;
}

输出结果:

fs demo:
------------------
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\DebugDir
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.cpp
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.vcxproj
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.vcxproj.filters
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.vcxproj.user
------------------
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.cpp
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.vcxproj
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.vcxproj.filters
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.vcxproj.user
------------------
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.exe.recipe
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.ilk
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.log
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.obj
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.tlog\CL.command.1.tlog
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.tlog\CL.read.1.tlog
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.tlog\CL.write.1.tlog
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.tlog\filesystem_test.lastbuildstate
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.tlog\link.command.1.tlog
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.tlog\link.read.1.tlog
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\filesystem_test.tlog\link.write.1.tlog
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\vc142.idb
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\Debug\vc142.pdb
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.cpp
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.vcxproj
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.vcxproj.filters
C:\Users\Kandy\Desktop\filesystem_test\filesystem_test\filesystem_test.vcxproj.user
------------------
okey=true------------------
okey=true
C:\Users\Kandy\Desktop\filesystem_test\Debug\filesystem_test.exe (进程 32060)已退出,代码为 0。
按任意键关闭此窗口. . .

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hellokandy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值