std::vector<std::string> Tool::getAllFilesWithIterator(const std::string& folderPath, bool recursive) {
	std::vector<std::string> fileList;

	try {
		std::filesystem::path path(folderPath);
		if (!std::filesystem::exists(path) || !std::filesystem::is_directory(path)) {
			return fileList;
		}

		if (recursive) {
			// 递归遍历所有子目录
			for (const auto& entry : std::filesystem::recursive_directory_iterator(path)) {
				if (entry.is_regular_file()) {
					fileList.push_back(entry.path().string());
				}
			}
		}
		else {
			// 仅遍历当前目录
			for (const auto& entry : std::filesystem::directory_iterator(path)) {
				if (entry.is_regular_file()) {
					fileList.push_back(entry.path().string());
				}
			}
		}
	}
	catch (const std::exception& e) {
		std::cerr << "文件遍历错误: " << e.what() << std::endl;
	}

	return fileList;
}

 

标签: 标准库, 文件目录

添加新评论