Skip to content

Last updated:2026-10-12

C++17 and SQLite Folder Size Statistics

Project Practice: Folder Size Calculation and Storage with C++17 and SQLite

This section unfolds around a project background: suppose your business needs to periodically check the size of certain folders and persist the results. How would you accomplish this project in C++?

This section walks you through a small project that uses C++17 and SQLite to implement a folder size calculation and storage system, helping you manage your file system better.

Project Goals

We will implement the following features: 1. Recursively calculate the size of a specified directory and its subdirectories. 2. Save the calculation results to an SQLite database for easy subsequent querying and analysis. 3. Highly extensible: you can easily modify the project to add multi-threading support, optimize performance, or store the results in another database.

Note: lazy version — the complete code has been updated to the Knowledge Planet community.

Project Implementation Steps

1. Environment

First, you need to make sure the following tools are installed in your development environment: - A C++17-capable compiler: such as g++; you can check the version with the following command:

g++ --version
Make sure g++ supports the C++17 standard.

  • SQLite3: we will use SQLite for database operations; you can install it via a package manager:
    sudo apt-get install sqlite3 libsqlite3-dev
    
    After installation, the SQLite development library libsqlite3-dev ensures we can use SQLite properly in C++.

2. Creating the Project Structure

We will split the code into several modules to keep a clear architecture: - folder_size.h / folder_size.cpp: responsible for recursively calculating the size of directories and their subdirectories. - database.h / database.cpp: encapsulates the SQLite database operation logic. - main.cpp: the main program entry, responsible for controlling the overall flow.

The project directory structure is as follows:

project/
│── folder_size.h
│── folder_size.cpp
│── database.h
│── database.cpp
└── main.cpp

3. Code Implementation

Next, we will implement each module step by step.

3.1 Directory Calculation Module

C++17 provides the std::filesystem library to recursively traverse directories and calculate sizes. So its core implementation is as follows:

uintmax_t total_size = 0;
for (const auto& entry : fs::recursive_directory_iterator(dir_path)) {
  if (fs::is_regular_file(entry.path())) {
    total_size += fs::file_size(entry.path());
  }
}

If you want to calculate the root directory, just calculate it once more.

3.2 SQLite Database Operations

Include:

#include <sqlite3.h>

Usage:

sqlite3_close(db_); // 关闭

// 插入
for (const auto& [folder, size] : folder_sizes) {
  const std::string insert_sql = 
      "INSERT OR REPLACE INTO FolderSizes (Folder, Size) "
      "VALUES ('" + folder + "', " + std::to_string(size) + ");";
  if (!ExecuteSQL(insert_sql)) {
    return false;
  }
}

// 执行
sqlite3_exec

4. Compiling and Running

Compiling the project is very simple:

mkdir build
cmake ../
make -j8

Then run the program to calculate the directory size and store it in the database:

./folder_size_calculator

The program will recursively calculate the size of the specified directory and save the results to the folder_sizes.db database.

5. Querying the Database

Finally, you can query the contents of the database through

the SQLite command-line tool:

sqlite3 folder_sizes.db
sqlite> SELECT * FROM FolderSizes;

This will display all the saved directories and their corresponding sizes.

End of this section

Comments