Skip to content
The Chat Archive
claude-sonnet-4-20250514· Anthropic LMArena Human Preference 140K

make it go recursive in folders

  1. User

    make it go recursive in folders

  2. Assistant

    Here's the updated version that recursively traverses all subdirectories:

    // list_folder_recursive.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];
        // Make sure the path ends with a slash (or backslash on Windows)
    #if defined(_WIN32)
        if (!folderPath.empty() && folderPath.back() != '\\')
            folderPath += '\\';
    #else
        if (!folderPath.empty() && folderPath.back() != '/')
            folderPath += '/';
    #endif
    
        // ------------------------------------------------------------------
        // 1. Build the file list automatically (RECURSIVE)
        // ------------------------------------------------------------------
        std::vector<std::filesystem::path> files;
        try {
            for (const auto& entry : std::filesystem::recursive_directory_iterator(folderPath)) {
                if (entry.is_regular_file())
                    files.push_back(entry.path());
            }
        } 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& filePath : files) {
            std::string fullPath = filePath.string();
            std::string relativePath = std::filesystem::relative(filePath, folderPath).string();
    
            FILE* inFile = fopen(fullPath.c_str(), "r");
            if (!inFile) {
                std::perror(("Error opening " + fullPath).c_str());
                continue;
            }
    
            // Header - shows the relative path from the base folder
            fprintf(outFile, "File: %s\n```\n", relativePath.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:

    1. std::filesystem::recursive_directory_iterator instead of the regular one - this automatically traverses all subdirectories
    2. Store full std::filesystem::path objects instead of just filenames, since we now have files in different subdirectories
    3. Show relative paths in the output using std::filesystem::relative() - so instead of just "File.swift", you'll see "File: SubFolder/File.swift" in the output

    Compile and run the same way:

    g++ -std=c++17 list_folder_recursive.cpp -o list_folder_recursive
    ./list_folder_recursive ./FitnessApp/Model/
    

    Now it will find and include files from ./FitnessApp/Model/Nutrition/, ./FitnessApp/Model/Exercise/, and any other subdirectories, showing their relative paths in the output file.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.