-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolderIconUpdater.cpp
More file actions
470 lines (408 loc) · 18.2 KB
/
FolderIconUpdater.cpp
File metadata and controls
470 lines (408 loc) · 18.2 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include <windows.h>
#include <shlobj.h>
#include <string>
#include <iostream>
#include <fstream>
#include <regex>
#include <filesystem>
// Helper function to convert UTF-8 string to wide string
static std::wstring Utf8ToWide(const std::string& utf8Str) {
int wideSize = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, nullptr, 0);
if (wideSize == 0) {
return L"";
}
std::wstring wideStr(wideSize - 1, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0], wideSize - 1);
return wideStr;
}
// Utility to extract folder name
std::wstring GetFolderName(const std::wstring& folderPath) {
return std::filesystem::path(folderPath).filename().wstring();
}
// Utility to extract file name
std::wstring GetFileName(const std::wstring& filePath) {
return std::filesystem::path(filePath).filename().wstring();
}
// Function to validate the icon file
static bool ValidateIconFile(const std::wstring& iconPath) {
if (!std::filesystem::exists(iconPath)) {
std::wcerr << L"Icon file not found: " << iconPath << std::endl;
return false;
}
std::wstring ext = std::filesystem::path(iconPath).extension();
if (ext != L".ico" && ext != L".dll") {
std::wcerr << L"Invalid icon file type: " << iconPath << L". Only .ico and .dll are supported." << std::endl;
return false;
}
return true;
}
// Function to validate and parse the /n argument
static int ParseIconIndex(const std::wstring& iconIndexStr) {
std::wregex numberRegex(LR"(^"?\d+"?$)");
if (!std::regex_match(iconIndexStr, numberRegex)) {
std::wcerr << L"Invalid icon index specified. /n must be a numeric value." << std::endl;
return -1;
}
return std::stoi(iconIndexStr);
}
// Function to prompt user for icon index
static int PromptIconIndex(const std::wstring& dllPath) {
int index = -1;
std::wcout << L"Enter the icon index for " << dllPath << L": ";
std::wcin >> index;
return index;
}
// Function to trim quotes from a string
static std::wstring TrimQuotes(const std::wstring& str) {
if (!str.empty() && str.front() == L'"' && str.back() == L'"') {
return str.substr(1, str.size() - 2);
}
return str;
}
static std::wstring ResolveRelativePath(const std::wstring& basePath, const std::wstring& relativePath) {
std::filesystem::path base(basePath);
std::filesystem::path resolved = base.parent_path() / relativePath;
return resolved.wstring();
}
// Function to read IconResource from desktop.ini and validate it
static std::wstring ReadAndValidateIconResource(const std::wstring& iniFilePath) {
std::wifstream iniFile(iniFilePath);
std::wstring line, iconResource;
if (!iniFile.is_open()) {
std::wcerr << L"Failed to open desktop.ini: " << iniFilePath << std::endl;
return L"";
}
// Read the desktop.ini file and find the IconResource entry
while (std::getline(iniFile, line)) {
if (line.find(L"IconResource=") == 0) {
iconResource = line.substr(13); // Extract value after "IconResource="
break;
}
}
iniFile.close();
if (iconResource.empty()) {
std::wcerr << L"No IconResource found in " << iniFilePath << std::endl;
return L"";
}
// Extract the icon file path from the IconResource value
size_t commaPos = iconResource.find_last_of(L",");
std::wstring iconPath = (commaPos != std::wstring::npos) ? iconResource.substr(0, commaPos) : iconResource;
// Trim quotes from the iconPath
iconPath = TrimQuotes(iconPath);
// Resolve relative path
if (!std::filesystem::path(iconPath).is_absolute()) {
iconPath = ResolveRelativePath(iniFilePath, iconPath);
}
// Check if the file exists
bool fileExists = std::filesystem::exists(iconPath);
if (!fileExists) {
std::wcerr << L"IconResource file does not exist: " << iconPath << std::endl;
}
// Validate the file extension
std::wstring extension = std::filesystem::path(iconPath).extension().wstring();
if (extension != L".ico" && extension != L".dll") {
std::wcerr << L"Unsupported IconResource file type in IconResource: "
<< L"\"" << GetFileName(iconPath) << L"\""
<< L" (Only .ico and .dll are supported.)" << std::endl;
// Even if the file doesn't exist, we still validate and warn about the type
}
// If the file doesn't exist or has an invalid type, return an empty string
if (!fileExists || (extension != L".ico" && extension != L".dll")) {
return L"";
}
return iconResource; // Return the valid IconResource string
}
// Function to apply attributes to a single file
static bool ApplyAttributes(const std::wstring& filePath, const std::wstring& attributeOption) {
DWORD attributes = GetFileAttributes(filePath.c_str());
if (attributes == INVALID_FILE_ATTRIBUTES) {
std::wcerr << L"Failed to get attributes for: " << filePath << std::endl;
return false;
}
// Apply +H, -H, +S, -S based on the attributeOption
if (attributeOption.find(L"+H") != std::wstring::npos) {
attributes |= FILE_ATTRIBUTE_HIDDEN;
}
if (attributeOption.find(L"-H") != std::wstring::npos) {
attributes &= ~FILE_ATTRIBUTE_HIDDEN;
}
if (attributeOption.find(L"+S") != std::wstring::npos) {
attributes |= FILE_ATTRIBUTE_SYSTEM;
}
if (attributeOption.find(L"-S") != std::wstring::npos) {
attributes &= ~FILE_ATTRIBUTE_SYSTEM;
}
// Apply the modified attributes to the file
if (SetFileAttributes(filePath.c_str(), attributes)) {
std::wcout << L"Attributes applied to: " << filePath << std::endl;
return true;
}
else {
std::wcerr << L"Failed to apply attributes to: " << filePath << std::endl;
return false;
}
}
// Helper function to parse attributes from /a
static DWORD ParseAttributes(const std::wstring& attributeOption) {
DWORD attributes = 0;
// Default to +H -S (Hidden, Not System) if no attributes specified
if (attributeOption.empty()) {
attributes |= FILE_ATTRIBUTE_HIDDEN;
attributes |= FILE_ATTRIBUTE_SYSTEM;
}
if (attributeOption.find(L"+H") != std::wstring::npos) {
attributes |= FILE_ATTRIBUTE_HIDDEN;
}
if (attributeOption.find(L"-H") != std::wstring::npos) {
attributes &= ~FILE_ATTRIBUTE_HIDDEN;
}
if (attributeOption.find(L"+S") != std::wstring::npos) {
attributes |= FILE_ATTRIBUTE_SYSTEM;
}
if (attributeOption.find(L"-S") != std::wstring::npos) {
attributes &= ~FILE_ATTRIBUTE_SYSTEM;
}
return attributes;
}
// Function to handle /a logic
static int HandleAttributes(const std::wstring& folderPath, const std::wstring& attributeOption) {
std::wstring iniFilePath = folderPath + L"\\desktop.ini";
if (!std::filesystem::exists(iniFilePath)) {
std::wcerr << L"desktop.ini not found in " << folderPath << std::endl;
return 1;
}
// Validate desktop.ini
std::wstring iconResource = ReadAndValidateIconResource(iniFilePath);
if (iconResource.empty()) {
std::wcerr << L"Invalid IconResource in desktop.ini. Skipping attribute changes." << std::endl;
return 1;
}
// Extract icon file path from IconResource
size_t commaPos = iconResource.find_last_of(L",");
std::wstring iconFilePath = (commaPos != std::wstring::npos) ? iconResource.substr(0, commaPos) : iconResource;
iconFilePath = TrimQuotes(iconFilePath);
// Resolve relative paths for the icon file
if (!std::filesystem::path(iconFilePath).is_absolute()) {
iconFilePath = ResolveRelativePath(iniFilePath, iconFilePath);
return 1;
}
// Validate the icon file
if (!ValidateIconFile(iconFilePath)) {
std::wcerr << L"Invalid IconResource file. Skipping attribute changes." << std::endl;
return 1;
}
// Apply attributes to desktop.ini and IconResource
ApplyAttributes(iniFilePath, attributeOption); // Pass the correct attribute options here
ApplyAttributes(iconFilePath, attributeOption); // Same for the icon
return 0;
}
// Function to update folder icon via SHChangeNotify
static bool UpdateFolderIcon(const std::wstring& folderPath, const std::wstring& iconPath, int iconIndex) {
SHFOLDERCUSTOMSETTINGS fcs = { 0 };
fcs.dwSize = sizeof(SHFOLDERCUSTOMSETTINGS);
fcs.dwMask = FCSM_ICONFILE;
fcs.pszIconFile = const_cast<LPWSTR>(iconPath.c_str());
fcs.iIconIndex = iconIndex;
HRESULT hr = SHGetSetFolderCustomSettings(&fcs, folderPath.c_str(), FCS_FORCEWRITE);
if (SUCCEEDED(hr)) {
SHChangeNotify(SHCNE_ATTRIBUTES, SHCNF_PATHW, folderPath.c_str(), NULL);
SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, folderPath.c_str(), NULL);
std::wcout << L"Folder icon updated successfully for \"" << GetFolderName(folderPath) << L"\"" << std::endl;
return true;
}
else {
std::wcerr << L"Failed to update folder icon for " << folderPath << L", HRESULT: " << hr << std::endl;
return false;
}
}
// Function to Desktop.ini UpdateTimestamp
void UpdateTimestamp(const std::wstring& filePath) {
HANDLE hFile = CreateFileW(filePath.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
FILETIME ft;
GetSystemTimeAsFileTime(&ft); // Current time
SetFileTime(hFile, NULL, NULL, &ft); // Update last write time
CloseHandle(hFile);
}
}
// Function to refresh or update folder icon
static int ProcessFolder(const std::wstring& folderPath, const std::wstring& iconPath, int iconIndex, const std::wstring& attributeOption) {
DWORD attributes = attributeOption.empty() ? INVALID_FILE_ATTRIBUTES : ParseAttributes(attributeOption);
std::wstring iniFilePath = folderPath + L"\\desktop.ini";
std::wstring iconResource;
DWORD originalAttributes = INVALID_FILE_ATTRIBUTES;
// Check if desktop.ini exists and get its attributes
if (std::filesystem::exists(iniFilePath)) {
originalAttributes = GetFileAttributes(iniFilePath.c_str());
if (originalAttributes == INVALID_FILE_ATTRIBUTES) {
std::wcerr << L"Failed to get attributes for desktop.ini: " << iniFilePath << std::endl;
return 1;
}
}
if (!iconPath.empty()) {
// Validate the icon file
if (!ValidateIconFile(iconPath)) {
return 1; // Return an error code if validation fails
}
// Determine the icon resource value
if (std::filesystem::path(iconPath).extension() == L".ico") {
iconIndex = (iconIndex == -1) ? 0 : iconIndex;
}
else if (std::filesystem::path(iconPath).extension() == L".dll") {
if (iconIndex == -1) {
iconIndex = PromptIconIndex(iconPath);
}
}
iconResource = iconPath + L"," + std::to_wstring(iconIndex);
// Update the folder icon
UpdateTimestamp(folderPath + L"\\desktop.ini");
UpdateTimestamp(iconPath);
UpdateFolderIcon(folderPath, iconPath, iconIndex);
// Apply attributes to desktop.ini and icon file if specified
if (!attributeOption.empty()) {
ApplyAttributes(iniFilePath, attributeOption); // Apply to desktop.ini
ApplyAttributes(iconPath, attributeOption); // Apply to icon file
}
}
else if (std::filesystem::exists(iniFilePath)) {
// Read the existing IconResource
iconResource = ReadAndValidateIconResource(iniFilePath);
if (iconResource.empty()) {
std::wcerr << L"An error found in IconResource." << std::endl;
return 1;
}
// Extract icon path and index from IconResource
size_t commaPos = iconResource.find_last_of(L",");
std::wstring existingIconPath = (commaPos != std::wstring::npos) ? iconResource.substr(0, commaPos) : iconResource;
int existingIconIndex = (commaPos != std::wstring::npos) ? std::stoi(iconResource.substr(commaPos + 1)) : 0;
// Refresh the folder icon
UpdateFolderIcon(folderPath, existingIconPath, existingIconIndex);
}
else {
std::wcerr << L"No desktop.ini found. Use /f /i to assign a folder icon." << std::endl;
UpdateFolderIcon(folderPath, iconPath, iconIndex);
return 1;
}
// Reapply the original attributes of desktop.ini, if it existed
if (originalAttributes != INVALID_FILE_ATTRIBUTES) {
if (!SetFileAttributes(iniFilePath.c_str(), originalAttributes)) {
std::wcerr << L"Failed to restore original attributes for desktop.ini: " << iniFilePath << std::endl;
return 1;
}
}
// If all operations succeed, return 0
return 0;
}
// Main function
int wmain(int argc, wchar_t* argv[]) {
if (argc == 1) { // No arguments were passed
// Construct the command to open cmd.exe with the help command
std::wstring currentExePath(MAX_PATH, L'\0');
GetModuleFileNameW(NULL, ¤tExePath[0], static_cast<DWORD>(currentExePath.size()));
currentExePath.resize(wcslen(currentExePath.c_str()));
// Command to open cmd.exe and execute the program with /?
std::wstring command = L"cmd.exe /k \"\"" + currentExePath + L"\" /?\"";
// Start cmd.exe with the command
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcessW(
NULL, &command[0], NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else {
std::wcerr << L"Failed to open cmd.exe. Error code: " << GetLastError() << std::endl;
return 3; // File or path not found
}
return 0; // Exit After launching CMD
}
// Existing argument parsing and functionality
std::wstring folderPath, iconPath, attributeOption;
int iconIndex = -1;
int result = 0; // Default to success
try {
for (int i = 1; i < argc; ++i) {
std::wstring arg = argv[i];
if (arg == L"/f" && i + 1 < argc) {
folderPath = argv[++i];
}
else if (arg == L"/i" && i + 1 < argc) {
iconPath = argv[++i];
}
else if (arg == L"/n" && i + 1 < argc) {
if (folderPath.empty() || iconPath.empty()) {
std::wcerr << L"Error: /n requires both /f and /i. Use /? for help." << std::endl;
return 1;
}
iconIndex = ParseIconIndex(argv[++i]);
if (iconIndex == -1) {
return 1;
}
}
// Additional argument handling
else if (arg == L"/a") {
std::wstring attributeOption;
if (i + 1 < argc && argv[i + 1][0] != L'/') {
attributeOption = argv[++i];
}
HandleAttributes(folderPath, attributeOption);
}
else if (arg == L"/?" || arg == L"-?" || arg == L"--help") {
std::wcout
<< L"Usage:\n\n"
<< L" FolderIconUpdater.exe /f <folder> [/i <icon_path>] [/n <icon_index>] [/a <attributes>]\n\n"
<< L"Options:\n"
<< L" /f: Specifies the folder whose icon will be updated.\n"
<< L" /i: Specifies the path to the icon file (.ico or .dll).\n"
<< L" /n: Specifies the icon index (applicable only for .dll files; optional for .ico files).\n"
<< L" /a: Specifies file attributes for \"desktop.ini\" and the icon file.\n"
<< L" Attributes:\n"
<< L" +H: Hidden -H: Not Hidden\n"
<< L" +S: System -S: Not System\n"
<< L" Default: +H -S (Hidden, Not System)\n\n"
<< L"Examples:\n\n"
<< L" 1. Refresh the folder icon based on existing desktop.ini:\n"
<< L" FolderIconUpdater.exe /f \"C:\\MyFolder\"\n\n"
<< L" 2. Assign an .ico file as the folder icon:\n"
<< L" FolderIconUpdater.exe /f \"C:\\MyFolder\" /i \"C:\\Icons\\Icon.ico\"\n\n"
<< L" 3. Assign an icon from a .dll file with a specific index:\n"
<< L" FolderIconUpdater.exe /f \"C:\\MyFolder\" /i \"C:\\Icons\\IconPack.dll\" /n 5\n\n"
<< L" 4. Assign an icon with specific file attributes:\n"
<< L" FolderIconUpdater.exe /f \"C:\\MyFolder\" /i \"C:\\Icons\\Icon.ico\" /a +H -S\n\n"
<< L"Notes:\n"
<< L" - If /a is used, the specified attributes will be applied to \"desktop.ini\" and the icon file.\n"
<< L" - If /f and /i are used without /a, \"desktop.ini\" and the icon file will default to +H -S.\n"
<< L" - If only /f is used, existing file attributes for \"desktop.ini\" and the icon file will remain unchanged.\n\n";
return 0;
}
else {
std::wcerr << L"Error: Unrecognized argument \"" << arg << L"\". Use /? for help." << std::endl;
return 2;
}
}
// Ensure /f is specified
if (folderPath.empty()) {
std::wcerr << L"Error: /f must be specified. Use /? for help." << std::endl;
return 1;
}
// Ensure /n is not used without /i
if (!iconPath.empty() && iconIndex == -1 && std::filesystem::path(iconPath).extension() == L".dll") {
iconIndex = PromptIconIndex(iconPath);
}
else if (iconIndex != -1 && iconPath.empty()) {
std::wcerr << L"Error: /n requires both /f and /i. Use /? for help." << std::endl;
return 1;
}
// Process the folder
result = ProcessFolder(folderPath, iconPath, iconIndex, attributeOption);
}
catch (const std::exception& e) {
std::wcerr << L"Unexpected error: " << Utf8ToWide(e.what()) << std::endl;
result = 1;
}
catch (...) {
std::wcerr << L"Unknown error occurred." << std::endl;
result = 1;
}
return result;
}