-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
274 lines (247 loc) · 6.64 KB
/
MainWindow.xaml.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
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Path = System.IO.Path;
namespace renameify
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
bool ShowPath = true;
bool HideHidden = true;
public MainWindow()
{
InitializeComponent();
}
#region UtilityFunctions
bool ValidateInput()
{
if (!IsValidFilename(PrependTextBox.Text))
{
MessageBox.Show("Illegal character in prepend text box");
return false;
}
if (!IsValidFilename(PrependTextBox.Text))
{
MessageBox.Show("Illegal character in the append text box");
return false;
}
return true;
}
private bool IsValidFilename(string testName)
{
Console.WriteLine(System.IO.Path.GetInvalidPathChars());
Regex containsABadCharacter = new Regex("[" + Regex.Escape(new string(System.IO.Path.GetInvalidPathChars())) + "/\\:"+ "]");
if (containsABadCharacter.IsMatch(testName)) { return false; };
return true;
}
private bool ShouldIgnore(string filepath)
{
string fDir = Path.GetDirectoryName(filepath);
string fName = Path.GetFileNameWithoutExtension(filepath);
string fExt = Path.GetExtension(filepath);
if (HideHidden)
{
if (fName.Length == 0) return true;
if (fName[0] == '.') return true;
}
if (IgnoreTextBox.Text.Length != 0)
{
if (fName.Contains(IgnoreTextBox.Text)) return true;
}
return false;
}
private void DirRename(string sDir)
{
if (!ValidateInput())
{
return;
}
//TODO need to make sure this works properly for all characters
//Currently thrown away may want to return them
List<String> oldfilenames = new List<String>();
List<String> newfilenames = new List<String>();
try
{
foreach (string f in Directory.GetFiles(sDir))
{
if (ShouldIgnore(f)) continue;
oldfilenames.Add(f);
File.Move(f, RenameItem(f));
newfilenames.Add(f);
}
foreach (string d in Directory.GetDirectories(sDir))
{
//recursive, if I'm going to return filenames need to add this in here
DirRename(d);
}
}
catch (System.Exception excpt)
{
MessageBox.Show(excpt.Message);
}
}
private List<String> DirSearch(string sDir)
{
List<String> files = new List<String>();
if (!ValidateInput())
{
return files;
}
try
{
//add every file in dir
foreach (string f in Directory.GetFiles(sDir))
{
if (ShouldIgnore(f)) continue;
files.Add(RenameItem(f, ShowPath));
}
//recursive add every file in subdirs
foreach (string d in Directory.GetDirectories(sDir))
{
files.AddRange(DirSearch(d));
}
}
catch (System.Exception excpt)
{
MessageBox.Show(excpt.Message);
}
return files;
}
private string RenameItem(string filepath, bool addFDir = true)
{
string fDir = Path.GetDirectoryName(filepath);
string fName = Path.GetFileNameWithoutExtension(filepath);
string fExt = Path.GetExtension(filepath);
if (FindTextBox.Text.Length > 0)
{
fName = fName.Replace(FindTextBox.Text, ReplaceTextBox.Text);
}
if(addFDir) return Path.Combine(fDir, String.Concat(PrependTextBox.Text, fName, AppendTextBox.Text, fExt));
else return Path.Combine(String.Concat(PrependTextBox.Text, fName, AppendTextBox.Text, fExt));
}
public void copyFiles(string sDir, string destDir, string text)
{
char[] seperators = { ',' };
List<string> filenames = new List<string>(text.Split(seperators));
Console.WriteLine(filenames);
foreach (string f in Directory.GetFiles(sDir))
{
string fName = Path.GetFileName(f);
Console.WriteLine(f);
if (filenames.Contains(fName))
{
Console.WriteLine("file is found here");
Console.WriteLine(f);
string destpath = Path.Combine(destDir, fName);
File.Copy(f, destpath);
}
}
foreach (string d in Directory.GetDirectories(sDir))
{
//recursive, if I'm going to return filenames need to add this in here
copyFiles(d, destDir, text);
}
}
public void preview()
{
if (Directory.Exists(FilePathTextBox.Text))
{
List<String> files = DirSearch(FilePathTextBox.Text);
lbFiles.Items.Clear();
foreach (string filename in files)
lbFiles.Items.Add(filename);
}
}
#endregion
#region xamlfunctions
private void LoadPath_Click(object sender, RoutedEventArgs e)
{
var dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
CommonFileDialogResult result = dialog.ShowDialog();
if (result == CommonFileDialogResult.Ok)
{
FilePathTextBox.Text = dialog.FileName;
}
else if (result == CommonFileDialogResult.Cancel)
{
//Do nothing its better to leave the text as is
//FilePathTextBox.Text = "";
}
preview();
}
private void LoadPathToCopyTo_Click(object sender, RoutedEventArgs e)
{
var dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
CommonFileDialogResult result = dialog.ShowDialog();
if (result == CommonFileDialogResult.Ok)
{
copyFromTextBox.Text = dialog.FileName;
}
else if (result == CommonFileDialogResult.Cancel)
{
//Do nothing its better to leave the text as is
//FilePathTextBox.Text = "";
}
preview();
}
private void SaveFiles_Click(object sender, RoutedEventArgs e)
{
if (Directory.Exists(FilePathTextBox.Text))
{
DirRename(FilePathTextBox.Text);
MessageBox.Show("Renamed files");
}
preview();
}
private void CopyFiles_Click(object sender, RoutedEventArgs e)
{
if (Directory.Exists(copyFromTextBox.Text) & Directory.Exists(FilePathTextBox.Text))
{
copyFiles(copyFromTextBox.Text, FilePathTextBox.Text, LewisStupidCopyBox.Text);
MessageBox.Show("Copied files");
}
preview();
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
#endregion
private void ShowPath_Checked(object sender, RoutedEventArgs e)
{
ShowPath = !ShowPath;
//reload the files
preview();
}
private void IncludeHiddenFiles_Checked(object sender, RoutedEventArgs e)
{
HideHidden = !HideHidden;
//reload the files
preview();
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
preview();
}
}
}