写在最前: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。
按任意键关闭此窗口. . .