make it go recursive in folders
- User
make it go recursive in folders
Assistant
// recursive_list_folder.cpp #include <cstdio> #include <cstring> #include <string> #include <vector> #include <filesystem> #include <iostream> int main(int argc, char* argv[]) { if (argc < 2) { std::cerr << "Usage: " << argv[0] << " <folder_path>\n"; return 1; } std::string folderPath = argv[1]; // ------------------------------------------------------------------ // 1. Build the file list recursively // ------------------------------------------------------------------ std::vector<std::string> files; try { for (const auto& entry : std::filesystem::recursive_directory_iterator(folderPath)) { if (entry.is_regular_file()) { files.push_back(entry.path().filename().string()); } } } catch (const std::filesystem::filesystem_error& e) { std::cerr << "Error reading directory: " << e.what() << '\n'; return 1; } if (files.empty()) { std::cerr << "No regular files found in " << folderPath << '\n'; return 1; } // ------------------------------------------------------------------ // 2. Open the output file and replicate your original logic // ------------------------------------------------------------------ FILE* outFile = fopen("prompt.txt", "w"); if (!outFile) { std::perror("Error opening prompt.txt"); return 1; } char buffer[1 << 16]; // 64 KiB line buffer for (const auto& file : files) { std::string fullPath = folderPath + file; FILE* inFile = fopen(fullPath.c_str(), "r"); if (!inFile) { std::perror(("Error opening " + fullPath).c_str()); continue; } // Header fprintf(outFile, "File: %s\n```\n", file.c_str()); // Copy the whole file out while (fgets(buffer, sizeof(buffer), inFile)) fputs(buffer, outFile); fprintf(outFile, "```\n\n"); // Footer fclose(inFile); } fclose(outFile); std::cout << "prompt.txt written with " << files.size() << " file(s).\n"; return 0; }Key changes:
std::filesystem::recursive_directory_iterator: This is the core change. Instead of a simpledirectory_iterator, we now userecursive_directory_iterator. This iterator automatically traverses all subdirectories within the givenfolderPath.- The rest of the logic remains the same, as it's responsible for handling each file found.
Compile and run as before:
g++ -std=c++17 recursive_list_folder.cpp -o recursive_list_folder ./recursive_list_folder ./FitnessApp/Model/Nutrition/This will now recursively list all files within the specified folder and its subfolders.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.