[C++] 纯文本查看 复制代码 #include <iostream>
#include <vector>
#include <filesystem>
#include <thread>
#include <mutex>
#include <string>
namespace fs = std::filesystem;
std::vector<std::string> resultFiles;
std::mutex resultMutex;
// 递归遍历文件夹的函数
void recursiveTraverse(const fs::path& directory) {
try {
for (const auto& entry : fs::directory_iterator(directory)) {
if (entry.is_directory()) {
// 遇到子文件夹,创建新线程处理
std::thread t(recursiveTraverse, entry.path());
t.detach();
} else if (entry.path().extension() == ".extt") {
// 找到 .extt 文件,添加到结果数组
std::lock_guard<std::mutex> lock(resultMutex);
resultFiles.push_back(entry.path().string());
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Filesystem error: " << e.what() << std::endl;
}
}
// 遍历所有磁盘的函数
void traverseAllDisks() {
for (char drive = 'A'; drive <= 'Z'; ++drive) {
fs::path rootPath = std::string(1, drive) + ":\\";
if (fs::exists(rootPath)) {
// 对每个存在的磁盘根目录创建线程进行遍历
std::thread t(recursiveTraverse, rootPath);
t.detach();
}
}
}
int main() {
// 开始遍历所有磁盘
traverseAllDisks();
// 等待一段时间,让线程有足够时间完成遍历
std::this_thread::sleep_for(std::chrono::seconds(60));
// 输出找到的 .extt 文件路径
std::cout << "Found .extt files:" << std::endl;
for (const auto& file : resultFiles) {
std::cout << file << std::endl;
}
return 0;
}
|