How can I find the size of all files located inside a folder?

How about letting OS do it for you:

long long int getFolderSize(string path) 
    // command to be executed
    std::string cmd("du -sb ");
    cmd.append(" | cut -f1 2>&1");

    // execute above command and get the output
    FILE *stream = popen(cmd.c_str(), "r");
    if (stream) {
        const int max_size = 256;
        char readbuf[max_size];
        if (fgets(readbuf, max_size, stream) != NULL) {
            return atoll(readbuf);
    // return error val
    return -1;

Leave a Comment