-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
169 lines (149 loc) · 6.25 KB
/
Program.cs
File metadata and controls
169 lines (149 loc) · 6.25 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
using System;
using System.Linq;
using System.Net;
namespace FileRipper
{
/** Program
* FileRipper's central Class
*/
class Program
{
#region Helpers
/** GetInput
* @param prompt - prompt for the user
* Returns the user's input based on the given prompt
*/
static string GetInput(string prompt)
{
try
{
Console.Write(prompt);
var result = "";
var line = Console.ReadLine();
// Process Line
if (!String.IsNullOrEmpty(line))
{
result = line.Trim();
}
return result;
}
catch (OverflowException e)
{
Console.WriteLine(e.Message + @" Please try again.");
return GetInput(prompt);
}
}
#endregion Helpers
/** Main
* @param args - Potential args that could be a directory or extension
* Executes and Processes FileRip depending on how user calls program
*/
static void Main(string[] args)
{
if (args == null) throw new ArgumentNullException("args");
var writer = LogWriter.Instance();
writer.LogFileRip("INFO", "Beginning FileRipper", Dns.GetHostName());
try
{
// Initialize FileRip Class
var fileRip = new FileRip();
var directory = "";
var extensions = "";
var ext = new string[0];
// Two Arguments
switch (args.Count())
{
case 2:
{
// Arguments
directory = args[0];
extensions = args[1];
if (extensions != null) ext = new [] {extensions};
}
break;
case 1:
{
// Argument
directory = args[0];
}
break;
case 0:
{
// No Arguments? Prompt User!
Console.WriteLine(@"Hello! Welcome to File Rip.");
// Directory Prompt
Console.WriteLine(@"Please input a Directory and hit enter.");
directory = GetInput("Directory: ");
// Extension Prompt
Console.WriteLine(@"Please input an extension and hit enter.");
Console.WriteLine(@" To search all extensions, leave blank.");
Console.WriteLine(@" To search multiple extensions, separate each one by a comma. (EX: ext1,ext2)");
extensions = GetInput("Extension: ");
if (extensions != null)
{
ext = extensions.Split(new [] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
}
// Proccess extensions string for logging
for (var i = 0; i < ext.Count(); i++)
{
if (i == 0)
{
extensions = ext[0]; // First term
}
else
{
extensions += (", " + ext[i]); // Other terms
}
}
}
break;
default:
if (args.Count() < 0) // Incorrect Number of Arguments
{
Console.WriteLine(@"Incorrect Number of Arguments.");
Console.WriteLine(@"FileRipper <directory path> <extension>");
writer.LogFileRip("ERROR", "Incorrect number of arguments used.", Dns.GetHostName());
}
else
{
// Initialize ext
ext = new string[args.Count()-1];
// Define Directory
directory = args[0];
// For each extension
for (var i = 1; i < args.Count(); i++)
{
ext[i - 1] = args[i];
if (i == 1) // First Term in series
{
extensions = args[i].Trim();
}
else // Everything else needs commas
{
extensions = extensions + ", " + args[i].Trim();
}
}
}
break;
}
// Changes to all extensions
if (extensions != null && extensions.Equals(""))
{
extensions = "All";
}
Console.WriteLine(@"Directory: {0} Extension(s): {1}", directory, extensions);
writer.LogFileRip("INFO", string.Format("Directory: {0}\tExtension(s): {1}", directory, extensions), Dns.GetHostName());
// Finds the Files (Returns List of FileRips)
fileRip.Rip(directory, ext);
Console.WriteLine(@"Done!");
writer.LogFileRip("INFO", "Done!", Dns.GetHostName());
Console.Read();
}
catch (ArgumentNullException e)
{
Console.WriteLine(@"ERROR: Null was used as input. " + e.Message);
writer.LogFileRip("ERROR", "Null was used as input. " + e.Message, Dns.GetHostName());
}
}
}
}