-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClearVSOutdatedPackage.cpp
154 lines (137 loc) · 3.83 KB
/
ClearVSOutdatedPackage.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
// ClearVSOutdatedPackage.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <algorithm>
typedef std::vector<std::string> StringList;
void split(const std::string& src, StringList& list, char separator = ' ')
{
std::string::size_type start = src.find_first_not_of(separator);
while (start != std::string::npos)
{
std::string::size_type end = src.find_first_of(separator, start);
if (end != std::string::npos)
{
list.push_back(std::string(src, start, end - start));
start = src.find_first_not_of(separator, end);
}
else
{
list.push_back(std::string(src, start, src.size() - start));
start = end;
}
}
}
class VersionCompare
{
public:
bool operator()(const std::string& lhs, const std::string& rhs)const
{
StringList lVals;
split(lhs, lVals, '.');
StringList rVals;
split(rhs, rVals, '.');
std::size_t count = std::min(lVals.size(), rVals.size());
for (std::size_t i = 0U; i < count; ++i)
{
int l = atoi(lVals[i].c_str());
int r = atoi(rVals[i].c_str());
if (l != r)
return l > r;
}
return lVals.size() > rVals.size();
}
};
typedef std::map<std::string, StringList, VersionCompare > VersionFileMap;
typedef std::unordered_map<std::string, VersionFileMap> FileValueMap;
int main(int argc, char *argv[])
{
if (argc < 2)
return -1;
char folderPath[MAX_PATH] = { 0 };
strcpy_s(folderPath, MAX_PATH, argv[1]);
if (strlen(argv[1]) == 1)
strcat_s(folderPath, MAX_PATH, ":");
strcat_s(folderPath, MAX_PATH, "\\*.*");
WIN32_FIND_DATAA FindFileData = { 0 };
HANDLE hFind = FindFirstFileA(folderPath, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
return -1;
FileValueMap fileValueMap;
do
{
if (strcmp(FindFileData.cFileName, ".") == 0 || strcmp(FindFileData.cFileName, "..") == 0)
continue;
if (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
std::string fileName;
std::string chip;
std::string language;
std::string version;
StringList vals;
split(FindFileData.cFileName, vals, ',');
for (StringList::iterator it = vals.begin(); it != vals.end(); ++it)
{
StringList v;
split(*it, v, '=');
if (v.size() == 2)
{
if (_stricmp(v[0].c_str(), "version") == 0)
version = v[1];
else if (_stricmp(v[0].c_str(), "chip") == 0)
chip = v[1];
else if (_stricmp(v[0].c_str(), "language") == 0)
language = v[1];
}
else
fileName = *it;
}
if (!fileName.empty() && !version.empty() )
{
std::string key = fileName + "_" + chip + "_" + language;
std::transform(key.begin(), key.end(), key.begin(), std::tolower);
fileValueMap[key][version].push_back(FindFileData.cFileName);
}
}
}while (FindNextFileA(hFind, &FindFileData));
FindClose(hFind);
for (FileValueMap::iterator itf = fileValueMap.begin(); itf != fileValueMap.end(); ++itf)
{
if (itf->second.size() <= 1)
continue;
bool first = true;
for (VersionFileMap::iterator itv = itf->second.begin(); itv != itf->second.end(); ++itv)
{
for (StringList::iterator it = itv->second.begin(); it != itv->second.end(); ++it)
{
std::cout << *it << std::endl;
if (!first)
{
char folderPath[MAX_PATH] = { 0 };
strcpy_s(folderPath, MAX_PATH, argv[1]);
if (strlen(argv[1]) == 1)
strcat_s(folderPath, MAX_PATH, ":");
strcat_s(folderPath, MAX_PATH, "\\");
strcat_s(folderPath, MAX_PATH, it->c_str());
SHFILEOPSTRUCTA FileOp = { 0 };
FileOp.fFlags = FOF_NOCONFIRMATION;
FileOp.pFrom = folderPath;
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
SHFileOperationA(&FileOp);
}
}
if (first)
{
std::cout << "---" << std::endl;
first = false;
}
}
std::cout << "********************" << std::endl;
}
return (0);
}