-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
219 lines (188 loc) · 5.97 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
#include <iostream>
#include <filesystem>
#include <string>
#include <thread>
#include <set>
#include <unordered_map>
#include <windows.h>
#include <shlobj.h>
namespace fs = std::filesystem;
// Ignoring temporary files and files that are being downloaded
// This is necessary because the program will try to move the file while it is being downloaded
// This will cause an error and the download will fail
// The program will try to move the file again when the download is complete with its normal extension
std::set<std::string> extensionsToIgnore = {
".crdownload",
".part",
".download",
".partial",
".ut",
".tmp",
".filepart",
".incomplete",
".!download",
};
// The use of wide strings is necessary for the program to work with non-ASCII characters
fs::path targetDirectory;
std::set<fs::path> previousFiles;
std::set<fs::path> currentFiles;
std::set<std::wstring> myExtensions;
std::unordered_map<std::wstring, std::wstring> myMap;
bool shouldIgnoreFile(const fs::path &filePath)
{
std::string extension = filePath.extension().string();
return extensionsToIgnore.count(extension) > 0;
}
bool detectChange(const fs::path &path)
{
currentFiles.clear();
for (const auto &entry : fs::directory_iterator(path))
if (entry.path().has_extension() && entry.is_regular_file() && !shouldIgnoreFile(entry.path()))
currentFiles.insert(entry.path());
if (previousFiles.empty())
{
previousFiles = currentFiles;
return true;
}
if (currentFiles == previousFiles)
return false;
myExtensions.clear();
previousFiles = currentFiles;
return true;
}
bool createFolder(const std::wstring &path)
{
if (fs::create_directory(path))
return true;
return false;
}
void toUpperCase(std::wstring &s)
{
for (int i = 0; i < s.size(); i++)
s[i] = std::towupper(s[i]);
}
void collectingExtensions()
{
try
{
for (auto const &dir_entry : currentFiles)
{
try
{
myExtensions.insert(dir_entry.extension().wstring()); // Use wide strings for extensions
}
catch (const std::exception &e)
{
std::wcout << L"Error collecting extensions: " << e.what() << std::endl;
}
}
}
catch (const std::exception &e)
{
std::wcout << L"Error in collectingExtensions: " << e.what() << std::endl;
}
}
void createFolderNames()
{
for (std::set<std::wstring>::iterator iter = myExtensions.begin(); iter != myExtensions.end(); iter++)
{
std::wstring extension = *iter;
extension = extension.substr(1);
toUpperCase(extension);
std::wstring folderName = extension + L"s";
createFolder(targetDirectory / fs::path(folderName));
myMap.insert({*iter, folderName});
}
}
bool moveToFile(const fs::path &oldpath, const fs::path &newpath)
{
try
{
// If the file already exists in the destination folder, rename it
// It is renamed as file (1) or file (2) etc.
int i = 0;
fs::path path = newpath;
while (fs::exists(path))
{
i++;
std::wstring newFilename = newpath.filename().wstring();
std::wstring extension = newFilename.substr(newFilename.find_last_of(L"."), newFilename.size());
newFilename = newFilename.substr(0, newFilename.find_last_of(L"."));
newFilename = newFilename + L" (" + std::to_wstring(i) + L")" + extension;
path = newpath.parent_path() / fs::path(newFilename);
}
fs::copy(oldpath, path);
fs::remove(oldpath);
return true;
}
catch (const fs::filesystem_error &e)
{
std::wcerr << L"Error moving the file: " << e.what() << std::endl;
return false;
}
}
void moveFiles()
{
try
{
for (auto const ¤tFile : currentFiles)
{
try
{
std::wstring extension = currentFile.extension().wstring(); // Use wide strings for extensions
std::wstring filename = currentFile.filename().wstring(); // Use wide strings for filenames
std::wstring folderName = myMap.at(extension);
std::wstring movePath = targetDirectory / fs::path(folderName) / fs::path(filename); // Use wide strings for paths
std::wstring oldPath = targetDirectory / fs::path(filename);
if (!moveToFile(oldPath, movePath))
std::wcout << L"\nFailed to move file: " << filename << std::endl;
}
catch (const std::exception &e)
{
std::wcout << L"Error moving a file: " << e.what() << std::endl;
}
}
}
catch (const std::exception &e)
{
std::wcout << L"Error in moveFiles: " << e.what() << std::endl;
}
}
std::wstring getDownloadsFolder()
{
PWSTR downloadsPath;
if (SHGetKnownFolderPath(FOLDERID_Downloads, 0, NULL, &downloadsPath) == S_OK)
{
std::wstring downloadsFolder(downloadsPath);
CoTaskMemFree(downloadsPath);
return downloadsFolder;
// std::wcout << L"Downloads folder path: " << downloadsFolder << std::endl;
}
else
{
std::cerr << "Failed to retrieve Downloads folder path." << std::endl;
return NULL;
}
}
int main()
{
// use custom folder path. Example:
// targetDirectory = L"D:\\OneDrive\\Downloads";
// Detect the Downloads folder path
// You have to include these libraries when compiling
// -lole32 -lshell32 -luuid
targetDirectory = getDownloadsFolder();
int sleepTime = 1;
while (true)
{
if (!detectChange(targetDirectory))
{
std::this_thread::sleep_for(std::chrono::minutes(sleepTime)); // No change detected, sleep
continue;
}
collectingExtensions();
createFolderNames();
moveFiles();
std::this_thread::sleep_for(std::chrono::minutes(sleepTime));
}
}