A Windows console application that lists file usage by grouping files based on their extensions. This program counts the total number of files and calculates their cumulative sizes.
- Lists files in a directory and groups them by their file extensions.
- Displays the total number of files per extension.
- Calculates the cumulative size of files for each extension.
- Simple, fast, and lightweight console-based interface.
For efficient and modern file management, the program utilizes the <filesystem> library, introduced in C++17. Compared to legacy and platform-specific methods (e.g. FindFirstFile, FindNextFile on Windows), which were often bulky and mistake-susceptible, this library introduces a standardized, cross-platform, and intuitive approach for handling files and directories.
-
Cross-Platform Compatibility: The
filesystemlibrary works seamlessly across Windows, Linux, and macOS, eliminating the need for platform-specific code. -
Ease of Use: This library streamlines straightforward and high-level APIs for common operations like iterating directories, retrieving file sizes, and filtering files based on extensions.
for (const auto& entry : std::filesystem::directory_iterator(dir_path)) { if (entry.is_regular_file()) { auto size{ entry.file_size() }; } }
-
Performance: Optimized for modern hardware,
filesystemoffers better performance compared to legacy implementations. -
Built-in Error Handling: This modern approach automatically throws exceptions for invalid paths, permission issues, etc. simplifying error management.
To improve the program's performance, a custom ThreadPool is used for concurrent execution. Directory enumeration and file metadata reads run in parallel, while each directory aggregates its results locally before one synchronized merge. This avoids lock contention for every file and keeps the scan safe when recursive tasks submit more work.
-
Parallel Task Execution: The
ThreadPoolmanages a pool of worker threads that handle tasks concurrently, ensuring faster file scanning. -
Efficient Resource Utilization: The number of threads is determined from the hardware's logical processors, with a minimum of one worker so scanning still works when the operating system cannot report the count.
size_t num_threads{ max<size_t>(1, thread::hardware_concurrency()) };
Usage: fileusage [--help] [-hdrsv(x regularexpression)] [folder]
switches:
h help
d reverse the order of listing
r suppress recursion
s sort by file sizes
v verbose
b benchmark
x filter with a regular expression
folder
starting folder or current folder if not specified