-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
46 lines (40 loc) · 1.4 KB
/
main.cpp
File metadata and controls
46 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* Author: Blake // MrDwarf7
* Date: 2025-06-13
* Description: This program removes empty directories from a specified folder.
* Usage: ./rm_stubs <folder_path>
* License: MIT License
*/
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main(const int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <folder_path>\n";
return 1;
};
const fs::path root{argv[1]};
try {
if (!fs::exists(root) || !fs::is_directory(root)) {
std::cerr << "Error: path does not exist or is not a directory.\n";
return 1;
};
/* actual processing here */
for (const auto &entry: fs::directory_iterator(root)) {
if (entry.is_directory()) {
if (const fs::path &subdir = entry.path(); fs::is_empty(subdir)) {
std::error_code error_code;
fs::remove(subdir, error_code);
if (error_code) {
std::cerr << "Failed to remove " << subdir << ": " << error_code.message() << "\n";
} else {
std::cout << "Removed empty directory" << subdir << " \n";
}
}
}
}
} catch (const fs::filesystem_error &e) {
std::cerr << "Filesystem error: " << e.what() << "\n";
return 1;
}
return 0;
}