-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
376 lines (353 loc) · 15.3 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <iostream>
#include <string>
#include <unordered_map>
#include "includes/diskutilization.h"
#include "includes/detectFiles.h"
#include "includes/deleteFiles.h"
void spaceAvail(){
std::vector<std::string> drives = GetLogicalDriveNames(); //diskutilization.h
for (const auto& drive : drives) {
std::string d = drive;
LPTSTR long_string = new TCHAR[d.size() + 1];
strcpy(long_string, d.c_str());
std::replace(d.begin(), d.end(), '\\', '/');
ULARGE_INTEGER freeBytesAvailable, totalNumberOfBytes,totalNumberOfFreeBytes;
if (GetDiskFreeSpaceEx(long_string, &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes)) { //windows.h
//Calculate the percentage of free space on the drive
double percentageFree = (static_cast<double>(freeBytesAvailable.QuadPart) / totalNumberOfBytes.QuadPart) * 100.0;
std::cout << drive<< std::endl;
std::cout << "Percentage of free space on drive: " << percentageFree << "%" << std::endl;
} else {
std::cerr << "Failed to retrieve disk space information." << std::endl;
}
std::cout << "======================================================" << std::endl;
}
}
void categorize() {
std::vector<std::string> drives = GetLogicalDriveNames(); //diskutilization.h
for (const auto& drive : drives) {
std::map<std::string, __int64> fileCategories;
std::map<std::string, long long> fileCount{
{"Videos", 0},
{"Images", 0},
{"Documents", 0},
{"Archives", 0},
{"Audios", 0},
{"Executables", 0},
{"Others", 0}
};
fileCategories["Videos"] = 0;
fileCategories["Images"] = 0;
fileCategories["Documents"] = 0;
fileCategories["Archives"] = 0;
fileCategories["Audios"] = 0;
fileCategories["Executables"] = 0;
fileCategories["Others"] = 0;
GetDirectorySizeAndCategorize(drive, fileCategories, fileCount); //diskutilization.h
std::cout << "Drive: " << drive << std::endl;
std::cout << "Space Utilization Breakdown:" << std::endl;
for (const auto& category : fileCategories) {
std::cout << category.first << " size: " << category.second << " bytes" << std::endl;
}
for (const auto& category : fileCount) {
std::cout << category.first << " count: " << category.second << std::endl;
}
std::cout << "=================================================================" << std::endl;
}
}
std::vector<std::vector<std::string>> detectDuplicateFolder(const fs::path directory_path){
std::vector<std::vector<std::string>> finalResult;
try {
std::map<std::string, std::vector<fs::path>> duplicate_files = find_duplicate_files(directory_path); //detectFiles.h
std::cout << "Duplicate files:" << std::endl;
for (const auto& entry : duplicate_files) {
if (entry.second.size() > 1) {
std::vector<std::string> temp;
for (const auto& file_path : entry.second) {
temp.push_back(file_path.string());
}
finalResult.push_back(temp);
}
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return {{}};
}
return finalResult;
}
void seperatevalues(std::string str,std::vector<std::string> &vec){
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, ',')) {
vec.push_back(token);
}
}
void askAndDelete(char opt,std::vector<std::string> result,std::unordered_map<int,std::string> paths){
opt = tolower(opt);
bool flag = false;
switch(opt){
case 'a': flag = deleteFiles(result); //deleteFiles.h
if(!flag){
std::cout<<"There is some error.\n";
}else{
std::cout<<"File succesfully deleted\n";
}
break;
case 'c': {
std::cout<<"Provide me with numbers associated to the file path(eg:{1}.C:/Users/) (input: 1,10,15)\n";
std::string dfn = ""; //delete filepath number
std::cin>>dfn;
std::vector<std::string> ftbd; //files to be deleted
seperatevalues(dfn,ftbd); //main.cpp
/*verify that the input is correct*/
std::vector<std::string> newresult;
for(auto &x : ftbd){
int val = stoi(x);
newresult.push_back(paths[val]);
}
flag = deleteFiles(newresult); //deleteFiles.h
if(!flag){
std::cout<<"There is some error.\n";
}else{
std::cout<<"File succesfully deleted\n";
}
}
break;
default: std::cerr<<"the input given is not appropriate.\n";
}
}
void duplicateFunctionality(char* argv[]){
try{
const char* filepath = argv[2];
std::vector<std::vector<std::string>> result = detectDuplicateFolder(filepath); //main.cpp
std::unordered_map<int,std::unordered_map<int,std::string>> whichduplicate;
int i = 1,j = 1;
if(result.size() > 0 && result[0].size() != 0){
for(auto &x : result){
std::cout<<"=============================== "<<i<<" =======================================\n";
for(auto &y : x){
std::cout<<j<<"."<<y<<std::endl;
whichduplicate[i][j] = y;
j++;
}
i++;
std::cout<<std::endl<<"=============================================================================="<<std::endl;
}
std::cout<<"Do you want to delete duplicate files(Y/N):";
char ch = 'N';
std::cin>>ch;
ch = toupper(ch);
if(ch == 'Y'){
std::cout<<"Do you want to delete all the duplicates or selected duplicates(a/c):\n";
char opt1 = 'a';
std::cin>>opt1;
opt1 = tolower(opt1);
switch(opt1){
case 'a': {
bool flag = true;
std::cout<<"\n*Caution:this will delete all the duplicate files shown and leave a random file of each duplicate group*\n";
std::cout<<"Do you want to continue(Y/N):\n";
char ch = 'Y';
std::cin>>ch;
ch = toupper(ch);
if(ch == 'Y'){
for(auto &x : result){
flag = flag && deleteDuplicateFiles(x); //deleteFiles.h
}
if(!flag){
std::cout<<"There is some error.\n";
}else{
std::cout<<"File succesfully deleted\n";
}
}
}
break;
case 'c': {
std::cout<<"enter the number associated with the duplicate file bunch:";
std::string res = "";
std::vector<std::string> duplis_no;
std::cin>>res;
seperatevalues(res,duplis_no); //main.cpp
std::vector<std::string> newresult;
for(auto &x : duplis_no){
std::cout<<"=============================== "<<x<<" =======================================\n";
std::cout<<"Provide me with numbers associated to the file path(eg:{1}.C:/Users/) (input: 1,10,15)\n";
std::string dfn = ""; //delete filepath number
std::cin>>dfn;
std::vector<std::string> temp;
seperatevalues(dfn,temp); //main.cpp
for(auto &it : temp){
newresult.push_back(whichduplicate[stoi(x)][stoi(it)]);
}
}
bool flag = deleteFiles(newresult); //deleteFiles.h
if(!flag){
std::cout<<"There is some error.";
}else{
std::cout<<"File succesfully deleted";
}
}
break;
default: std::cerr<<"you have entered a wrong option";
}
}
}
}catch(...){
std::cerr<<"please provide a path of the folder.";
}
}
void dctdelLargeFunctionality(char *argv[]){
try{
const char* dirpath= argv[2];
std::vector<std::string> result;
try{
long long maxsize = atoi(argv[3]) * 1024 * 1024;
result = detectLargeFiles(dirpath,maxsize); //detectFiles.h
std::unordered_map<int,std::string> paths;
int i = 1;
for(auto &x : result){
std::cout<<i<<"."<<x<<std::endl;
paths[i++] = x;
}
if(result.size() != 0){
std::cout<<"Do You Want to Delete these files(Y/N)";
char ch = 'N';
std::cin>>ch;
ch = toupper(ch);
if(ch == 'Y'){
char opt = 'a';
std::cout<<"Do you want to delete all the files or select some files to delete:(a/c)\n";
std::cin>>opt;
askAndDelete(opt,result,paths); //main.cpp
}
}
}catch(...){
result = detectLargeFiles(dirpath); //detectFiles.h
std::unordered_map<int,std::string> paths;
int i = 1;
for(auto &x : result){
std::cout<<i<<"."<<x<<std::endl;
paths[i++] = x;
}
for(auto &x : result){
std::cout<<x<<std::endl;
}
if(result.size() != 0){
std::cout<<"Do You Want to Delete these files(Y/N)";
char ch = 'N';
std::cin>>ch;
ch = toupper(ch);
if(ch == 'Y'){
char opt = 'a';
std::cout<<"Do you want to delete all the files or select some files to delete:(a/c)\n";
std::cin>>opt;
askAndDelete(opt,result,paths); //main.cpp
}
}
}
}catch(...){
std::cerr<<"please provide a path of the folder.";
}
}
void extensionFunctionality(char *argv[]){
try{
const char* dirpath= argv[2];
std::vector<std::string> result;
try{
std::string ext = argv[3];
result = detectFileByExtension(dirpath,ext); //detectFile.h
std::unordered_map<int,std::string> paths;
int i = 1;
for(auto &x : result){
std::cout<<i<<"."<<x<<std::endl;
paths[i++] = x;
}
if(result.size()){
std::cout<<"Do You Want to Delete these files(Y/N)";
char ch = 'N';
std::cin>>ch;
ch = toupper(ch);
if(ch == 'Y'){
char opt = 'a';
std::cout<<"Do you want to delete all the files or select some files to delete:(a/c)\n";
std::cin>>opt;
askAndDelete(opt,result,paths); //main.cpp
}
}
}catch(...){
std::cerr<<"please provide a extension(eg: .cpp, .json)";
}
}catch(...){
std::cerr<<"please provide a path of the folder.";
}
}
void showCommands(){
std::cout<<"Available Commands"<<std::endl;
std::cout<<"space-avail Arguments: None"<<std::endl;
std::cout<<"categorize Arguments: None"<<std::endl;
std::cout<<"detect-duplicate-folder Arguments: Folder Path"<<std::endl;
std::cout<<"delete-duplicate Arguments: Folder Path"<<std::endl;
std::cout<<"detect-large-files Arguments: Folder Path,(optional)file size"<<std::endl;
std::cout<<"delete-large Arguments: Folder Path,(optional)file size"<<std::endl;
std::cout<<"detect-same-ext-file Arguments: Folder Path, Extension"<<std::endl;
std::cout<<"delete-by-ext Arguments: Folder Path, Extension"<<std::endl;
std::cout<<"folder-breakdown Arguments: Folder Path"<<std::endl;
}
int main(int argc, char* argv[]){
std::string cmd;
try{
cmd = argv[1];
}catch(...){
cmd = "help";
}
std::unordered_map<std::string,int> mp = {
{"help",1},
{"space-avail",2},
{"categorize",3},
{"detect-duplicate",4},
{"delete-duplicate",9},
{"detect-large-files",5},
{"delete-large-files",8},
{"detect-same-ext-file",6},
{"delete-same-ext-file",7},
{"folder-breakdown",10}
};
int val = 0;
switch(mp[cmd]){
case 1: showCommands(); //main.cpp
break;
case 2: spaceAvail(); //main.cpp
break;
case 3: categorize(); //main.cpp
break;
case 4: /*detect and then asks for deletion*/
duplicateFunctionality(argv); //main.cpp
break;
case 5: /*detects large file and then asks for deletion*/
dctdelLargeFunctionality(argv); //main.cpp
break;
case 6: /*detects file with same extension and then asks for deletion*/
extensionFunctionality(argv); //main.cpp
break;
case 7: /*detects file with same extension and then asks for deletion*/
extensionFunctionality(argv); //main.cpp
break;
case 8: /*detects large file and then asks for deletion*/
dctdelLargeFunctionality(argv); //main.cpp
break;
case 9: /*detect and then asks for deletion*/
duplicateFunctionality(argv); //main.cpp
break;
case 10: {
try{
const char* dirpaths = argv[2];
folderBifurcation(dirpaths,true); //detectFiles.h
}catch(...){
std::cerr<<"directory parameter empty";
}
}
break;
default: std::cerr<<"Command does-not exist please use 'diskcleaner help' to see all commands";
}
return 0;
}