-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
308 lines (281 loc) · 11.3 KB
/
Program.cs
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
using System.IO;
namespace InstallDebloater
{
using System.Text.RegularExpressions;
using IniParser;
using IniParser.Model;
class Program
{
// Root folder of the game/application
private static string? RootFolder;
private static OperatingSystem os = Environment.OSVersion;
private static PlatformID pid = os.Platform;
private static int FileCounter, FolderCounter = 0;
private static bool IsRelative, IsNamingScheme, IsFolder;
private static double StartingFileSize = 0;
private static double FinalFileSize = 0;
private static double TotalFileSize = 0;
static void Main(string[] args)
{
try
{
var ini = new FileIniDataParser();
IniData data = ini.ReadFile(args[0]);
Parse(data);
StartingFileSize = DefineSize(RootFolder);
if (!IsRelative && !IsNamingScheme && !IsFolder)
{
Console.WriteLine("No actions specified. Ending the process.");
System.Environment.Exit(0);
}
if (IsRelative)
{
DeleteRelative(args[0]);
}
if (IsNamingScheme)
{
DeleteNamingScheme(args[0]);
}
if (IsFolder)
{
DeleteFolder(args[0]);
}
if (FileCounter == 0 && FolderCounter == 0)
{
Output.failmessage(FileCounter, FolderCounter);
}
else
{
FinalFileSize = DefineSize(RootFolder);
Output.successmessage(StartingFileSize, FileCounter, FolderCounter, TotalFileSize, FinalFileSize);
}
Console.ReadLine();
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Index out of bounds. Please provide a commandline parameter.");
Console.ReadLine();
}
catch (IniParser.Exceptions.ParsingException)
{
Console.WriteLine("Could not parse file. Is the path/format correct? " + System.IO.Path.Combine(Directory.GetCurrentDirectory(), args[0]));
Console.ReadLine();
}
catch (FileNotFoundException)
{
Console.WriteLine("Could not read file. Is the path/filename valid? " + System.IO.Path.Combine(Directory.GetCurrentDirectory(), args[0]));
Console.ReadLine();
}
}
/// <summary>
/// Processes all values from IniData.
/// </summary>
private static void Parse(IniData d)
{
RootFolder = d["CORE"]["ROOT"];
IsRelative = bool.Parse(d["CORE"]["RELATIVE"]);
IsNamingScheme = bool.Parse(d["CORE"]["NAMING_SCHEME"]);
IsFolder = bool.Parse(d["CORE"]["FOLDER"]);
}
/// <summary>
/// Calculates the total size of all folders and files. Returns that value.
/// </summary>
private static double DefineSize(string folderpath)
{
double value = 0;
var dirs = Directory.GetDirectories(folderpath, "*", System.IO.SearchOption.AllDirectories);
foreach (var dir in dirs)
{
var subfiles = Directory.GetFiles(dir);
foreach (var subfile in subfiles)
{
value += new FileInfo(subfile).Length;
}
}
var files = Directory.GetFiles(folderpath);
foreach (var file in files)
{
value += new FileInfo(file).Length;
}
return value;
}
/// <summary>
/// Checks if the OS is Windows. If not, strings get converted. Returns the string.
/// </summary>
private static string[] DefinePlatform(string[] list)
{
if (pid != PlatformID.Win32NT)
{
Regex pattern = new Regex("[\\\\]");
for (int i = 0; i <= list.Count() - 1; i++)
{
try
{
list[i] = pattern.Replace(list[i], "/");
}
catch (ArgumentException)
{
}
}
}
return list;
}
/// <summary>
/// Deletes files in the *_RELATIVE.txt document. (Files that are relative to ROOT)
/// </summary>
private static void DeleteRelative(string arg)
{
string relative = arg.Substring(0, arg.LastIndexOf(".")) + "_RELATIVE.txt";
string[] file = DefinePlatform(System.IO.File.ReadAllLines(relative));
foreach (string line in file)
{
try
{
if (line.Substring(0, 1).Equals("#") || line.Trim().Equals(""))
{
continue;
}
try
{
TotalFileSize += new FileInfo(System.IO.Path.Combine(RootFolder, line)).Length;
System.IO.File.Delete(System.IO.Path.Combine(RootFolder, line));
Console.WriteLine("Deleted: " + System.IO.Path.Combine(RootFolder, line));
FileCounter++;
}
catch (FileNotFoundException)
{
Console.WriteLine("Could not find file: " + line);
}
}
catch (ArgumentOutOfRangeException)
{
}
}
}
/// <summary>
/// Deletes files in the *_NAMING_SCHEME.txt document. (Files with a specific structure)
/// A partial folder structure that ends with a target file works too.
/// </summary>
private static void DeleteNamingScheme(string arg)
{
string scheme = arg.Substring(0, arg.LastIndexOf(".")) + "_NAMING_SCHEME.txt";
string[] schemefile = DefinePlatform(System.IO.File.ReadAllLines(scheme));
foreach (string line in schemefile)
{
try
{
if (line.Substring(0, 1).Equals("#") || line.Trim().Equals(""))
{
continue;
}
var folders = Directory.GetDirectories(RootFolder, "*", System.IO.SearchOption.AllDirectories);
foreach (var folder in folders)
{
var files = Directory.GetFiles(folder);
foreach (var file in files)
{
if (file.Contains(line))
{
Console.WriteLine("Deleting: " + file);
TotalFileSize += new FileInfo(file).Length;
System.IO.File.Delete(file);
FileCounter++;
}
}
try
{
var path = System.IO.Path.Combine(RootFolder, folder, line);
TotalFileSize += new FileInfo(path).Length;
System.IO.File.Delete(path);
Console.WriteLine("Deleting: " + path);
FileCounter++;
}
catch (FileNotFoundException)
{
}
catch (DirectoryNotFoundException)
{
}
}
var rootfiles = Directory.GetFiles(RootFolder);
foreach (var rootfile in rootfiles)
{
if (rootfile.Contains(line))
{
Console.WriteLine("Deleting: " + rootfile);
TotalFileSize += new FileInfo(rootfile).Length;
System.IO.File.Delete(rootfile);
FileCounter++;
}
}
}
catch (ArgumentOutOfRangeException)
{
}
}
}
/// <summary>
/// Deletes folders in the *_FOLDER.txt document. (Folders and their files that are relative to ROOT)
/// Also includes subfolders & files.
/// </summary>
private static void DeleteFolder(string arg)
{
string folderlist = arg.Substring(0, arg.LastIndexOf(".")) + "_FOLDER.txt";
string[] folderfile = DefinePlatform(System.IO.File.ReadAllLines(folderlist));
foreach (string folder in folderfile)
{
var fullpath = System.IO.Path.Combine(RootFolder, folder);
try
{
if (folder.Substring(0, 1).Equals("#") || folder.Trim().Equals(""))
{
continue;
}
try
{
System.IO.Directory.Delete(fullpath);
Console.WriteLine("Deleting: " + folder);
FolderCounter++;
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("Could not find folder: " + fullpath);
}
catch (IOException)
{
var subfolders = Directory.GetDirectories(fullpath, "*", System.IO.SearchOption.AllDirectories);
Array.Sort(subfolders);
Array.Reverse(subfolders);
foreach (var subfolder in subfolders)
{
var subfiles = Directory.GetFiles(subfolder);
foreach (var subfile in subfiles)
{
TotalFileSize += new FileInfo(subfile).Length;
System.IO.File.Delete(subfile);
Console.WriteLine("Deleting " + subfile);
FileCounter++;
}
System.IO.Directory.Delete(subfolder);
FolderCounter++;
}
var files = Directory.GetFiles(fullpath);
foreach (var file in files)
{
TotalFileSize += new FileInfo(file).Length;
System.IO.File.Delete(file);
Console.WriteLine("Deleting " + file);
FileCounter++;
}
Console.WriteLine("Deleting: " + fullpath);
System.IO.Directory.Delete(fullpath);
FolderCounter++;
}
}
catch (ArgumentOutOfRangeException)
{
}
}
}
}
}