#通过路径信息判断属于哪个磁盘,计算剩余内存空间
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
fs::path path = "your_folder_or_file_path"; // 替换成你要检查的路径
std::string disk = fs::absolute(path).root_directory().string(); // 获取该路径所属的磁盘名称
std::cout << "路径 " << path << " 属于磁盘 " << disk << std::endl;
uintmax_t space = fs::space(path).available; // 获取该磁盘的可用空间
std::cout << "剩余内存空间: " << space << " 字节" << std::endl;
return 0;
}
#使用C++17实现删除文件夹的功能
#include <filesystem>
#include <iostream>
int main() {
std::filesystem::path folderPath = "path_to_your_folder"; // 指定要删除的文件夹路径
if (std::filesystem::exists(folderPath) && std::filesystem::is_directory(folderPath)) {
std::filesystem::remove_all(folderPath); // 使用remove_all函数递归删除文件夹及其中的所有内容
std::cout << "文件夹删除成功" << std::endl;
} else {
std::cout << "指定的路径不存在或不是文件夹" << std::endl;
}
return 0;
}
#判断文件夹目录是否存在,不存在就创建
bool isExistFolder(std::string folderPath)
{
path path1(folderPath);
if (!exists(path1)) //必须先检测路径是否存在才能使用文件入口.
{
create_directories(path1); //直接创建一个目录结构
}
directory_entry entry(path1); //文件入口
if (entry.status().type() == file_type::directory) //强枚举类型
{
return true;
}
return false;
}
##计算某个文件夹的内存大小
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
fs::path folderPath = "your_folder_path"; // 替换成你要检查的文件夹路径
uintmax_t totalSize = 0;
for (const auto& entry : fs::recursive_directory_iterator(folderPath)) {
if (fs::is_regular_file(entry.status())) {
totalSize += fs::file_size(entry.path());
}
}
std::cout << "文件夹占用的内存大小为: " << totalSize << " 字节" << std::endl;
return 0;
}