-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
112 lines (94 loc) · 3.23 KB
/
main.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <filesystem>
#include <algorithm>
#include <vector>
namespace fs = std::filesystem;
struct FileType
{
std::string name;
std::vector<std::string> extensions;
};
const std::vector<FileType> FILE_TYPES = {
{"Images", {".jpg", ".jpeg", ".png", ".gif"}},
{"Documents", {".txt", ".doc", ".docx", ".pdf"}},
{"Videos", {".mp4", ".avi", ".mov"}},
{"Music", {".mp3", ".wav", ".aac"}},
{"Archives", {".zip", ".tar", ".gz", ".rar"}}};
class FileOrganizer
{
public:
FileOrganizer(std::string directory_path) : directory_path_m(directory_path) {}
void organize()
{
for (auto &dir_entry : std::filesystem::directory_iterator{directory_path_m})
{
if (!dir_entry.is_regular_file())
continue;
std::string file_ext = dir_entry.path().extension();
std::transform(file_ext.begin(), file_ext.end(), file_ext.begin(),
[](unsigned char c)
{ return std::tolower(c); });
bool type_found = false;
for (const FileType &type : FILE_TYPES)
{
for (const std::string &ext : type.extensions)
{
if (file_ext == ext)
{
move_file_to_type_directory(dir_entry.path().filename().string(), type);
type_found = true;
}
if (type_found)
break;
}
if (type_found)
break;
}
}
}
private:
std::string directory_path_m;
void move_file_to_type_directory(const std::string &file_name, const FileType &type)
{
std::string source_file = directory_path_m + "/" + file_name;
std::string target_dir = directory_path_m + "/" + type.name;
std::string target_file = target_dir + "/" + file_name;
std::filesystem::create_directory(target_dir);
if (fs::exists(fs::path(target_file)))
{
target_file = resolve_conflict(target_file);
}
std::rename(source_file.c_str(), target_file.c_str());
std::cout << source_file << " --> " << target_file << std::endl;
}
std::string resolve_conflict(std::string file_name)
{
int counter = 1;
fs::path file_path = fs::path(file_name);
std::string new_name = file_name;
while (fs::exists(fs::path(new_name)))
{
new_name = file_path.parent_path().string() + "/" + file_path.stem().string() +
"(" + std::to_string(counter) + ")" + file_path.extension().string();
counter++;
}
return new_name;
}
};
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cout << "Usage: organize <directory_path>" << std::endl;
return 1;
}
std::string directory_path = argv[1];
if (!fs::exists(directory_path) || !fs::is_directory(directory_path))
{
std::cout << "Error: Directory '" << directory_path << "' does not exist." << std::endl;
return 1;
}
FileOrganizer fo{directory_path};
fo.organize();
std::cout << "Files organized successfully!" << std::endl;
}