-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
354 lines (317 loc) · 11.7 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
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
//using System;
//using System.IO;
//using System.Threading;
namespace FileAccess
{
class Program
{
public struct Variables
{
public bool recur;
public string dirPath;
public string logFilePath;
public int accessDelay;
public void print()
{
Console.WriteLine($"1. Recursive: {recur}");
Console.WriteLine($"2. Directory path to search: \"{dirPath}\"");
Console.WriteLine($"3. Log File: \"{logFilePath}\"");
Console.WriteLine($"4. Delay (seconds) between file accesses: {accessDelay}");
}
/*
* Parses the given input and decides what the user wants to change.
* It then gets the users input and updates the appropriate variable.
*/
public void parseUserInput(int selection)
{
string result;
Console.Clear();
if (selection == 1)
{
result = getUserInput("Recursive (Y/N)", false).ToUpper();
if (result == "Y")
{
recur = true;
}
else
{
recur = false;
}
}
else if (selection == 2)
{
result = getUserInput("Input path to desired search directory.", true);
if (Directory.Exists(result))
{
dirPath = result;
}
else
{
Console.WriteLine($"Directory: {result} not found.\n");
}
}
else if (selection == 3)
{
result = getUserInput("Input path to desired log file.", true);
if (result == "")
{
Console.WriteLine("No path entered.\n");
}
else
{
logFilePath = result;
}
}
else if (selection == 4)
{
bool loop = true;
while (loop)
{
result = getUserInput("Input delay, in seconds, between file accesses.", false);
int delay;
if (int.TryParse(result, out delay))
{
accessDelay = delay;
loop = false;
}
else
{
Console.WriteLine("Unexpected input. Please try again.\n");
}
}
}
else
{
Console.WriteLine("Unknown input entered. Please make a new selection.\n");
}
Console.Clear();
}
}
static void Main(string[] args)
{
Console.Clear();
Variables variables = parseArgs(args);
Thread.Sleep(3000);
Console.Clear();
Console.WriteLine("Executing with the following variables:");
variables.print();
Thread.Sleep(5000);
using (StreamWriter sw = File.AppendText(variables.logFilePath))
{
sw.Write("\nBeginning File modifation and renaming.\n\n");
}
List<string> allFiles = GetFilesFromPath(variables.dirPath, variables.recur);
Console.Clear();
Console.WriteLine("\nBeginning file modification and renaming.\n");
foreach (string file in allFiles)
{
if (file == variables.logFilePath)
{
Console.WriteLine("Ignoring log file.");
continue; //Don't modify the log file in any way unless to update with a new action
}
Thread.Sleep(variables.accessDelay * 1000);
string[] tmpFile = file.Split('.');
if (tmpFile[tmpFile.Length - 1] == "txt") //We only want to modify txt files, since adding a newline char won't break them
{
ModifyFile(file, "\n", variables.logFilePath);
}
else //Anything that is not a .txt file will be renamed
{
UpdateFileName(file, ".bk", variables.logFilePath);
}
}
Console.WriteLine("\n\nFinished with file modification.\n\nBeginning cleanup...\n");
Thread.Sleep(5000);
Console.Clear();
cleanup(variables);
}
/*
* Collects a list of all the files in the given path
* If "recur" is set to true, it will do a recursive search on all directories that are found in the given path.
*/
static List<string> GetFilesFromPath(string path, bool recur)
{
List<string> files = new List<string>();
try
{
Console.WriteLine($" \nCollecting files from: {path}\n");
Thread.Sleep(1000);
files.AddRange(Directory.GetFiles(path));
Console.WriteLine("Files found: ");
files.ToList().ForEach(Console.WriteLine);
if (recur)
{
Console.WriteLine("Searching recurisivly in directories.");
foreach (string dir in Directory.GetDirectories(path))
{
files.AddRange(GetFilesFromPath(dir, recur));
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
return files;
}
/*
* Used to modify files by appending {newLine} to the end of each file given
*/
static bool ModifyFile(string path, string newLine, string logFile)
{
if (!File.Exists(path))
{
Console.WriteLine("File: {0} does not exist.", path);
return false;
}
try
{
using (StreamWriter sw = File.AppendText(path))
{
sw.Write(newLine);
LogActivity($"File: {path} appended with \\n char.", logFile);
}
return true;
}
catch (UnauthorizedAccessException e)
{
LogActivity($"Could not modify {path} due to error: {e}", logFile);
Console.WriteLine(e);
return false;
}
}
/*
* Renames the file using a new extension that is provided
*/
static bool UpdateFileName(string path, string newExtension, string logFile)
{
System.IO.FileInfo fi = new System.IO.FileInfo(path);
if (fi.Exists)
{
fi.MoveTo((path += newExtension));
LogActivity($"File {getCleanFileName(path)} renamed to: {path}", logFile);
return true;
}
return false;
}
/*
* Used to log all activity that is done by this program.
* This should make it easier to ensure that everything can be cleaned up at the end.
*/
static void LogActivity(string message, string path)
{
Console.WriteLine(message);
System.IO.File.AppendAllText(path, (message += "\n"));
}
/*
* Checks if there are any args sent to the program and shows the menu if requested.
*/
static Variables parseArgs(string[] args)
{
Variables variables = new Variables();
//Set the default variables
variables.recur = false;
variables.logFilePath = @".\log.txt";
variables.dirPath = @".\";
variables.accessDelay = 0;
//If true, the user would like to update some of the variables.
if (args.Length > 0)
{
if (args[0] == "-menu")
{
while (true)
{
printMenu(variables);
int selection;
selection = Convert.ToInt16(Console.ReadLine());
if (selection == 0)
{
Console.WriteLine("Commiting Variables...");
Thread.Sleep(2000);
return variables;
}
else
{
variables.parseUserInput(selection);
}
}
}
}
return variables;
}
/*
* Print the menu for user input.
*/
static void printMenu(Variables variables)
{
Console.WriteLine("File Access menu. Please input the number corresponding to the setting you would like to update.");
Console.WriteLine("0. Commit variables and run program.");
variables.print();
Console.WriteLine("Please enter your selection: ");
}
/*
* Helper function to read user input and help with input validation.
*/
static string getUserInput(string message, bool isPath)
{
string result;
Console.WriteLine(message);
if (isPath)
{
result = @"" + Console.ReadLine();
}
else
{
result = "";
bool loop = true;
while (loop)
{
result += Console.ReadLine();
if (result == "")
{
Console.WriteLine("Please input a response.");
}
else
{
loop = false;
}
}
}
return result;
}
static void cleanup(Variables variables)
{
using (StreamWriter sw = File.AppendText(variables.logFilePath))
{
sw.Write("\n\nFinished with file modification. Beginning cleanup.\n");
}
List<string> files = GetFilesFromPath(variables.dirPath, variables.recur);
Thread.Sleep(5000);
Console.Clear();
Console.WriteLine("\nBeginning cleanup of renamed files.\n");
Thread.Sleep(2500);
files.AddRange(Directory.GetFiles(variables.dirPath));
foreach(string file in files)
{
string[] chkFileName = file.Split(".");
if (chkFileName[(chkFileName.Length-1)] == "bk")
{
System.IO.FileInfo fi = new System.IO.FileInfo(file);
if (fi.Exists)
{
string newFileName = getCleanFileName(file);
fi.MoveTo(newFileName);
LogActivity($"File {file} renamed to: {newFileName}", variables.logFilePath);
}
}
Thread.Sleep(1500);
}
Console.WriteLine("\nFile cleanup finished.");
}
static string getCleanFileName(string fileName)
{
return fileName.Substring(0, fileName.Length-3);
}
}
}