-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
335 lines (281 loc) · 11.8 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// { g++ main.cpp -o main -lole32 -loleaut32 -luuid -lshlwapi }
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept> // Äîáàâëåí çàãîëîâî÷íûé ôàéë äëÿ std::runtime_error
#include <windows.h> // Äîáàâëåí çàãîëîâî÷íûé ôàéë äëÿ SetConsoleTextAttribute
#include <shlobj.h>
#include <ObjBase.h>
#include "filesystem.hpp"
#include "fs_std.hpp"
// îáðàùàòüñÿ ÷åðåç fs::*
#define FOREGROUND_YELLOW (FOREGROUND_RED | FOREGROUND_GREEN)
#define FOREGROUND_WHITE (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "ole32.lib")
using namespace std;
// Ñòðóêòóðà äëÿ õðàíåíèÿ êàòåãîðèé è ñîîòâåòñòâóþùèõ ðàñøèðåíèé
struct Category
{
string name;
vector<string> extensions;
};
// Ñïèñîê êàòåãîðèé
vector<Category> categories{
{"Video", {".mp4", ".mov", ".avi", ".mkv", ".wmv", ".3gp", ".3g2", ".mpg", ".mpeg", ".m4v", ".h264", ".flv", ".rm", ".swf", ".vob"}},
{"Code", {".py", ".js", ".html", ".jar", ".json", ".java", ".cpp", ".css", ".php"}},
{"Data", {".sql", ".sqlite", ".sqlite3", ".csv", ".dat", ".db", ".log", ".mdb", ".sav", ".tar", ".xml", ".xlsx", ".xls", ".xlsm", ".ods"}},
{"Audio", {".mp3", ".wav", ".ogg", ".flac", ".aif", ".mid", ".midi", ".mpa", ".wma", ".wpl", ".cda", ".aac", ".m4a"}},
{"Images", {".jpg", ".png", ".bmp", ".ai", ".psd", ".ico", ".jpeg", ".ps", ".svg", ".tif", ".tiff", ".gif", ".eps"}},
{"Archives", {".zip", ".rar", ".7z", ".z", ".gz", ".rpm", ".arj", ".pkg", ".deb", ".tar.gz", ".tar.bz2"}},
{"Text", {".pdf", ".txt", ".doc", ".docx", ".rtf", ".tex", ".wpd", ".odt"}},
{"Presentations", {".pptx", ".ppt", ".pps", ".key", ".odp"}},
{"Fonts", {".otf", ".ttf", ".fon", ".fnt"}},
{"Installers", {".torrent", ".msi", ".exe"}},
{"Mobile", {".apk", ".obb"}},
{"Backups", {".bak", ".bak2"}},
{"Other", {}}};
// Êîíñòàíòû äëÿ áîëåå ÿñíîãî êîäà
const char PATH_SEPARATOR = '/';
const string DEFAULT_CATEGORY = "Other";
// Ôóíêöèÿ äëÿ ñîçäàíèÿ êàòåãîðèàëüíûõ ïàïîê
void createCategoryFolders(const fs::path &folderPath)
{
for (const auto &cat : categories)
{
fs::path path = folderPath / cat.name;
fs::create_directory(path);
}
}
// Ôóíêöèÿ äëÿ ïåðåìåùåíèÿ ôàéëà â ñîîòâåòñòâóþùóþ ïàïêó
void moveFileToCategory(const fs::path &filePath)
{
try
{
// Ïîëó÷àåì èìÿ ôàéëà è åãî ðàñøèðåíèå
string fileName = filePath.filename().string();
string extension = filePath.extension().string();
// Èùåì êàòåãîðèþ, ñîîòâåòñòâóþùóþ ðàñøèðåíèþ ôàéëà
auto it = find_if(categories.begin(), categories.end(),
[&](const Category &c)
{
return find(c.extensions.begin(),
c.extensions.end(),
extension) != c.extensions.end();
});
// Åñëè êàòåãîðèÿ íå íàéäåíà, èñïîëüçóåì êàòåãîðèþ ïî óìîë÷àíèþ
Category *category = it != categories.end() ? &*it : nullptr;
if (category == nullptr)
{
category = &categories.back();
}
// Ñòðîèì ïóòü ê íîâîìó ìåñòó ôàéëà
fs::path destPath = filePath.parent_path() / category->name;
// Èñïðàâëÿåì ñëåøè â ïóòè (åñëè íåîáõîäèìî)
destPath = destPath.make_preferred();
// Åñëè ïàïêà íå ñóùåñòâóåò, ñîçäàåì å¸
if (!fs::exists(destPath))
{
fs::create_directory(destPath);
}
// Ïðîâåðÿåì, íå ñóùåñòâóåò ëè óæå ôàéë ñ òàêèì èìåíåì â ïàïêå
for (int i = 1; fs::exists(destPath / fileName); ++i)
{
fileName = filePath.stem().string() + "_" + to_string(i) + extension;
}
// Ñòðîèì îêîí÷àòåëüíûé ïóòü äëÿ ôàéëà
fs::path finalPath = destPath / fileName;
finalPath = finalPath.make_preferred();
// Ïåðåèìåíîâûâàåì ôàéë
fs::rename(filePath, finalPath);
// Âûâîäèì èíôîðìàöèþ î ïåðåìåùåíèè ôàéëà
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE);
cout << "Move: " << fileName << " to " << category->name << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_WHITE);
}
catch (const exception &e)
{
// Âûâîäèì ñîîáùåíèå îá îøèáêå ïðè âîçíèêíîâåíèè èñêëþ÷åíèÿ
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cerr << "Error while moving file: " << e.what() << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_WHITE);
// Ìîæíî äîáàâèòü äîïîëíèòåëüíûå äåéñòâèÿ, òàêèå êàê ïðîòîêîëèðîâàíèå èëè âûâîä ïîëüçîâàòåëþ
}
}
// Ôóíêöèÿ äëÿ ðåêóðñèâíîãî óäàëåíèÿ ïóñòûõ ïàïîê â çàäàííîé äèðåêòîðèè
void removeEmptyFolders(const fs::path &folderPath)
{
try
{
// Ïðîõîäèì ïî âñåì ýëåìåíòàì â òåêóùåé äèðåêòîðèè
for (const auto &entry : fs::directory_iterator(folderPath))
{
// Åñëè òåêóùèé ýëåìåíò ÿâëÿåòñÿ äèðåêòîðèåé
if (entry.is_directory())
{
// Ðåêóðñèâíî âûçûâàåì removeEmptyFolders äëÿ âëîæåííîé äèðåêòîðèè
removeEmptyFolders(entry.path());
}
}
// Ïðîâåðÿåì, ÿâëÿåòñÿ ëè òåêóùàÿ äèðåêòîðèÿ ïóñòîé
if (fs::is_empty(folderPath))
{
// Ïîëó÷àåì èìÿ òåêóùåé êàòåãîðèè èç èìåíè äèðåêòîðèè
auto categoryName = folderPath.filename().string();
// Ïîèñê ñîîòâåòñòâóþùåé êàòåãîðèè ïî èìåíè
auto it = find_if(categories.begin(), categories.end(), [&](const Category &cat) {
return cat.name == categoryName;
});
// Óäàëÿåì òåêóùóþ äèðåêòîðèþ è âûâîäèì ñîîáùåíèå, åñëè ýòî íå êàòåãîðèàëüíàÿ äèðåêòîðèÿ
fs::remove_all(folderPath);
if (it == categories.end())
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE);
cout << "Remove empty folder: " << folderPath.filename() << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_WHITE);
}
}
}
catch (const exception &e)
{
// Âûâîäèì ñîîáùåíèå îá îøèáêå ïðè âîçíèêíîâåíèè èñêëþ÷åíèÿ
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cerr << "Error while removing empty folders: " << e.what() << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_WHITE);
}
}
// Ôóíêöèÿ äëÿ ñîðòèðîâêè ôàéëîâ â ïàïêå
void sortFiles(const fs::path &folderPath)
{
try
{
// Ñîçäàåì êàòåãîðèàëüíûå ïàïêè
createCategoryFolders(folderPath);
// Ïåðåìåùàåì ôàéëû
for (const auto &entry : fs::directory_iterator(folderPath))
{
if (entry.is_regular_file())
{
moveFileToCategory(entry.path());
}
}
// Óäàëÿåì âñå ïóñòûå ïàïêè, êðîìå êàòåãîðèàëüíûõ
removeEmptyFolders(folderPath);
}
catch (const exception &e)
{
// Âûâîäèì ñîîáùåíèå îá îøèáêå ïðè âîçíèêíîâåíèè èñêëþ÷åíèÿ
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cerr << "Error while sorting files: " << e.what() << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_WHITE);
}
}
fs::path getFolderPathFromExplorer()
{
// Îòêðûòèå äèàëîãîâîãî îêíà ïðîâîäíèêà äëÿ âûáîðà ïàïêè
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
IFileOpenDialog *pFileOpen;
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void **>(&pFileOpen));
if (SUCCEEDED(hr))
{
DWORD dwOptions;
pFileOpen->GetOptions(&dwOptions);
pFileOpen->SetOptions(dwOptions | FOS_PICKFOLDERS);
// Îòîáðàæåíèå äèàëîãîâîãî îêíà
hr = pFileOpen->Show(NULL);
if (SUCCEEDED(hr))
{
IShellItem *pItem;
hr = pFileOpen->GetResult(&pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
if (SUCCEEDED(hr))
{
fs::path selectedPath(pszFilePath);
CoTaskMemFree(pszFilePath);
pItem->Release();
pFileOpen->Release();
return selectedPath;
}
}
}
pFileOpen->Release();
}
CoUninitialize();
return fs::path(); // Âîçâðàùàåì ïóñòîé ïóòü, åñëè âûáîð ïàïêè îòìåíåí èëè ïðîèçîøëà îøèáêà
}
int main()
{
system("chcp 1251");
while (true)
{
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_YELLOW);
std::cout << "\n------- Íà÷àëî ðàáîòû -------\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_WHITE);
std::cout << "Ââåäèòå ïóòü ê ïàïêå. Íàæìèòå enter äëÿ âûáîðà ïàïêè Download. \n";
std::string mainPath = "";
while (mainPath.empty())
{
std::cout << "Ïóòü ê ïàïêå: ";
std::getline(std::cin, mainPath);
if (mainPath.empty())
{
CoInitialize(nullptr);
CHAR linkPathDef[MAX_PATH];
HRESULT result = SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, linkPathDef);
if (result != S_OK)
std::cout << "Error: " << result << "\n";
else
strcat_s(linkPathDef, "\\Downloads"); // Äîáàâèì ê ïóòè "Downloads"
mainPath = linkPathDef;
std::cout << "Âûáðàíà ïàïêà Download!\n";
CoUninitialize();
}
else
{
// Ïðîâåðêà íàëè÷èÿ êàâû÷åê â íà÷àëå è êîíöå ïóòè
if (!mainPath.empty() && (mainPath.front() == '\"' || mainPath.front() == '\'') &&
(mainPath.back() == '\"' || mainPath.back() == '\''))
{
// Óáèðàåì êàâû÷êè èç íà÷àëà è êîíöà ïóòè
mainPath = mainPath.substr(1, mainPath.length() - 2);
}
}
cout << mainPath << endl;
if (!fs::path(mainPath).is_absolute())
{
std::cout << "Ââåäåííûé ïóòü íå ÿâëÿåòñÿ äåéñòâèòåëüíûì. Ïîïðîáóéòå ñíîâà.\n";
mainPath = "";
continue;
}
// Ïðåîáðàçóåì ñòðîêó â îáúåêò std::filesystem::path
fs::path fsMainPath = mainPath;
// Ïðåîáðàçîâûâàåì ïóòü ê ïðåäïî÷òèòåëüíîìó ôîðìàòó
fsMainPath.make_preferred();
// Äîáàâëåíà ïðîâåðêà íà ñóùåñòâîâàíèå ïàïêè
if (!mainPath.empty() && !fs::exists(fsMainPath))
{
cerr << "Error: Folder does not exist." << endl;
return 1;
}
// Âûçûâàåì ôóíêöèþ ñîðòèðîâêè, ïåðåäàâàÿ std::filesystem::path
sortFiles(fsMainPath);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_YELLOW);
std::cout << "------- Êîíåö ðàáîòû -------\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_WHITE);
}
std::cout << "\nÆåëàåòå ïðîäîëæèòü? (y/n): ";
std::string choice;
std::getline(std::cin, choice);
if (choice != "y" && choice != "í")
{
system("cls");
break;
}
}
return 0;
}