-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenamerForm.cs
More file actions
248 lines (220 loc) · 9.13 KB
/
RenamerForm.cs
File metadata and controls
248 lines (220 loc) · 9.13 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
using System;
using System.Windows.Forms;
using System.IO;
using Microsoft.WindowsAPICodePack.Dialogs;
namespace LJM_Utils
{
public partial class RenamerForm : Form
{
System.Drawing.Point OptionGroupLocation = new System.Drawing.Point(11, 202);
int PreviousStrategySelection = -1;
public RenamerForm()
{
InitializeComponent();
// Resize form and move option groups to overlay one another.
// Reasoning: keeping everything separated allows for easy design of the controls
// when they are intended to all be on top of one another during runtime.
this.Width = 360;
groupRandomOptions.Location = OptionGroupLocation;
groupRegExOptions.Location = OptionGroupLocation;
groupLinearOptions.Location = OptionGroupLocation;
}
private void ValidateRenameProcess(ref bool NoErrorsFound)
{
// validate given path
if (!Directory.Exists(txtInputDirectory.Text))
{
NoErrorsFound = false;
MessageBox.Show($"The given path \"{txtInputDirectory.Text}\" does not exist.",
"Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// validate options for selected strategy
switch (comboRenamingStrat.SelectedIndex)
{
case (int)StrategySelection.Linear:
if (txtLinearExample.Text.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
{
NoErrorsFound = false;
}
break;
case (int)StrategySelection.Random:
if (numRandomCharsOption.Value < 1)
{
NoErrorsFound = false;
}
break;
case (int)StrategySelection.RegEx:
// TODO: given pattern containins a capture group
NoErrorsFound = false; // not implemented
break;
default:
NoErrorsFound = false;
break;
}
// validate given extensions
if (!true)
{
}
// validate combo box selection
if (comboRenamingStrat.SelectedItem.ToString() == Vocab.Renaming.ComboDefault)
{
NoErrorsFound = false;
MessageBox.Show($"You must select a renaming strategy.",
"Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnRename_Click(object sender, EventArgs e)
{
bool NoErrorsFound = true;
ValidateRenameProcess(ref NoErrorsFound);
if (NoErrorsFound)
{
try
{
switch (comboRenamingStrat.SelectedItem.ToString())
{
case Vocab.Renaming.ComboLinear:
{
LinearRenamer linearRenamer = new LinearRenamer(txtInputDirectory.Text, getExtInclusions(),
txtPrefixOption.Text, txtSuffixOption.Text);
linearRenamer.Execute();
MessageBox.Show($"Success! \n{linearRenamer.FileCount} files were renamed.", "Notice",
MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
case Vocab.Renaming.ComboRandom:
{
RandomRenamer randomRenamer = new RandomRenamer(txtInputDirectory.Text, getExtInclusions());
randomRenamer.Execute();
MessageBox.Show($"Success! \n{randomRenamer.FileCount} files were renamed.", "Notice",
MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
case Vocab.Renaming.ComboRegEx:
{
MessageBox.Show("RegEx strategy is WIP and not available.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
default:
break;
}
}
catch (Exception thrownException)
{
MessageBox.Show($"An unhandled exception has occurred: {thrownException.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txtInputDirectory.Text = "";
txtExtInclusions.Text = "";
txtPrefixOption.Text = "";
txtSuffixOption.Text = "";
numRandomCharsOption.Value = 10;
}
private void btnUndo_Click(object sender, EventArgs e)
{
// TODO: write support for logging of renames and functionality to undo logged "runs"
// Idea 1: logging folder in temp folder with all rename jobs as separate text
// files, timestamped with time since epoch as "job number"
// Idea 2: SQLite database thats lists everything
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void RenamerForm_Load(object sender, EventArgs e)
{
comboRenamingStrat.SelectedItem = "Select a strategy...";
}
private void btnBrowseForInput_Click(object sender, EventArgs e)
{
// Folder browser improved dialog source: https://stackoverflow.com/a/41511598
CommonOpenFileDialog dialog = new CommonOpenFileDialog
{
IsFolderPicker = true,
EnsurePathExists = true
};
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
txtInputDirectory.Text = dialog.FileName;
}
}
private string[] getExtInclusions()
{
if (txtExtInclusions.Text.Trim() != "")
{
return txtExtInclusions.Text.Split(' ');
}
else
{
return null;
}
}
public enum StrategySelection
{
Default = 0,
Linear = 1,
Random = 2,
RegEx = 3
}
private void comboRenamingStrat_SelectedIndexChanged(object sender, EventArgs e)
{
if (PreviousStrategySelection == -1 && comboRenamingStrat.SelectedIndex == 0)
{
PreviousStrategySelection = 0;
}
else
{
// hide previous group
switch (PreviousStrategySelection)
{
case (int)StrategySelection.Default:
break;
case (int)StrategySelection.Linear:
groupLinearOptions.Visible = false;
break;
case (int)StrategySelection.Random:
groupRandomOptions.Visible = false;
break;
case (int)StrategySelection.RegEx:
groupRegExOptions.Visible = false;
break;
default:
break;
}
// show selected group
switch (comboRenamingStrat.SelectedIndex)
{
case (int)StrategySelection.Default:
break;
case (int)StrategySelection.Linear:
groupLinearOptions.Visible = true;
break;
case (int)StrategySelection.Random:
groupRandomOptions.Visible = true;
break;
case (int)StrategySelection.RegEx:
groupRegExOptions.Visible = true;
break;
default:
break;
}
}
PreviousStrategySelection = comboRenamingStrat.SelectedIndex;
}
private void txtPrefixOption_TextChanged(object sender, EventArgs e){UpdateLinearExample();}
private void txtSuffixOption_TextChanged(object sender, EventArgs e){UpdateLinearExample();}
private void UpdateLinearExample()
{
txtLinearExample.Text = LinearRenamer.ExampleFilename(txtPrefixOption.Text, txtSuffixOption.Text);
}
private void numRandomCharsOption_ValueChanged(object sender, EventArgs e)
{
txtRandomExample.Text = RandomRenamer.ExampleFilename((int)numRandomCharsOption.Value);
}
}
}