-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumCheckFrm.cs
262 lines (248 loc) · 8.18 KB
/
NumCheckFrm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace bvhSet
{
public partial class NumCheckFrm : Form
{
public NumCheckFrm()
{
InitializeComponent();
richTextBox1.Text = ReadBF();
}
private string ReadBF()
{
string txt = "";
if (File.Exists("bvh.txt"))
{
string[] bf = File.ReadAllLines("bvh.txt");
foreach (string line in bf)
{
txt += line.Trim() + "\n";
}
}
return txt;
}
private void button2_Click(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
rtbReport.Text = "";
lblMsg.Text = "";
if (Directory.Exists(tbSearchDir.Text))
{
treeView1.Nodes.Add(FileNodes(tbSearchDir.Text));
treeView1.ExpandAll();
}
else
{
lblMsg.Text = "Invalid Path";
}
}
public static TreeNode FileNodes(string inFilePath)
{
TreeNode root = new TreeNode();
root.Text = inFilePath;
if (Directory.Exists(inFilePath))
{
foreach (string directory in Directory.EnumerateDirectories(inFilePath))
{
root.Nodes.Add(FileNodes(directory));
}
foreach (string file in Directory.EnumerateFiles(inFilePath))
{
if (file.ToString().ToLower().EndsWith(".bvh"))
{
TreeNode tn = new TreeNode();
tn.Text = file.ToString();
root.Nodes.Add(tn);
}
}
}
return root;
}
private string IsNode(string inFile)
{
bool match = false;
string retVal = "";
string[] fsplit = inFile.Split('\\');
string numCheck="";
if (fsplit.Length > 1 && inFile.ToLower().Contains(".bvh"))
{
numCheck = fsplit[fsplit.Length - 1].Replace(".bvh","");
}
if (numCheck != "")
{
string[] rtSplit = richTextBox1.Text.Split('\n');
for (int i = 0; i < rtSplit.Length; i++)
{
string[] rowSplit = rtSplit[i].Split(' ');
if (rowSplit.Length > 0)
{
if (rowSplit[0].ToLower().Contains(numCheck))
{
retVal = rtSplit[i].Replace(" ", "_").Replace(",", "-").Replace("\t", "_").Replace("#","-");
match = true;
}
}
}
}
else
{
retVal += "numcheck not determined";
}
return retVal + " ("+ numCheck + "-" + match.ToString() +")";
}
private void btnCompare_Click(object sender, EventArgs e)
{
string rept = "";
if (treeView1.Nodes.Count > 0)
{
foreach (TreeNode node in treeView1.Nodes)
{
rept = PreviewNode(node,false);
}
}
rtbReport.Text = rept;
}
public string PreviewNode(TreeNode inNode, bool commit)
{
string rept = "";
if (inNode.Nodes.Count == 0)
{
if (commit)
{
rept += RenameNode(inNode.Text);
}
else
{
rept += string.Format("Rename {0} to {1}", inNode.Text, IsNode(inNode.Text)) + "\n";
}
}
else
{
rept += "Directory " + inNode.Text + "\n";
foreach (TreeNode tn in inNode.Nodes)
{
rept += PreviewNode(tn, commit); ;
}
}
return rept;
}
private string RenameNode(string inFile)
{
string[] invalidFileChars = new string[] { " ", ",", "\t", "\n", "\r", "#", "/", "'", "@", "?", "*", "$", "=", "+", "&", "^", "[", "{", ":", ";", "<", ">","\"" };
bool match = false;
string retVal = "";
string[] fsplit = inFile.Split('\\');
string numCheck = "";
string newFilePath="";
string newFileName = "";
if (fsplit.Length > 1 && inFile.ToLower().Contains(".bvh"))
{
numCheck = fsplit[fsplit.Length - 1].Replace(".bvh", "");
}
if (numCheck != "")
{
string[] rtSplit = richTextBox1.Text.Split('\n');
for (int i = 0; i < rtSplit.Length; i++)
{
string[] rowSplit = rtSplit[i].Split(' ');
if (rowSplit.Length > 0)
{
if (rowSplit[0].ToLower().Contains(numCheck))
{
newFileName = rtSplit[i];
foreach (string ifc in invalidFileChars)
{
newFileName = newFileName.Replace(ifc, "_");
}
match = true;
}
}
}
}
else
{
retVal += "numcheck not determined";
}
if (File.Exists(inFile) && match && numCheck != "" && !inFile.ToLower().Contains(newFileName))
{
try
{
newFilePath = inFile.ToLower().Replace(numCheck.ToLower() + ".bvh", newFileName + ".bvh");
File.Copy(inFile, newFilePath);
File.Delete(inFile);
retVal = " Renamed (" + numCheck + "-" + newFileName + match.ToString() + ")";
}
catch (Exception ex)
{
if (ex.ToString().ToLower().Contains("invalid char"))
{
retVal = string.Format("{0} Bad File Char", numCheck);
}
else
{
retVal = string.Format("{0} Error:{1}", numCheck, ex.ToString());
}
}
}
else
{
string err = "";
if (inFile.ToLower().Contains(newFileName))
{
err = " already renamed.";
}
else if (numCheck == "")
{
err = "Missing file info";
}
else
{
err = "..Skip..invalid file?";
}
retVal = string.Format("{0} {1}", numCheck, err);
}
return retVal;
}
private void button1_Click(object sender, EventArgs e)
{
string rept = "";
foreach (TreeNode tn in treeView1.Nodes)
{
rept += PreviewNode(tn, true);// ProcessNode(tn);
}
rtbReport.Text = rept;
}
public string ProcessNode(TreeNode inNode)
{
if (inNode.Nodes.Count == 0)
{
return string.Format("Rename {0} to {1}", inNode.Text, RenameNode(inNode.Text)) + "\n";
}
else
{
foreach (TreeNode childNode in inNode.Nodes)
{
return ProcessNode(childNode);
}
}
return "";
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
lblMsg.Text = treeView1.SelectedNode.Text;
if (treeView1.SelectedNode.Nodes.Count > 0)
{
tbSearchDir.Text = treeView1.SelectedNode.Text;
button2_Click(button2, EventArgs.Empty);
}
}
}//class
}