-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
312 lines (274 loc) · 10.9 KB
/
Form1.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace regexRename
{
public partial class Form1 : Form
{
List<FileInfo> fileList = new List<FileInfo>();
List<string> newNameList = new List<string>();
List<int> nowState = new List<int>();
string[] stateList = { "", "成功", "失败", "目标文件(夹)名已存在", "具有只读属性", "目标路径不合法" };
public Form1()
{
InitializeComponent();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else
e.Effect = DragDropEffects.None;
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
Array inputfilelist = (System.Array)e.Data.GetData(DataFormats.FileDrop);
foreach (string i in inputfilelist)
{
if (!Directory.Exists(i) || (Directory.Exists(i) && checkBox_includeDir.Checked ))
{
FileInfo newfile = new FileInfo(i);
if (!fileList.Exists(file => file.FullName.Equals(newfile.FullName, StringComparison.OrdinalIgnoreCase)))
{
fileList.Add(newfile);
newNameList.Add("");
nowState.Add(0);
}
}
if (Directory.Exists(i) && checkBox_includeSubFiles.Checked )
{
if(checkBox_includeDir.Checked)
{
DirectoryInfo[] Directories = new DirectoryInfo(i).GetDirectories("*", SearchOption.AllDirectories);
foreach (DirectoryInfo newfile in Directories)
{
if (!fileList.Exists(file => file.FullName.Equals(newfile.FullName, StringComparison.OrdinalIgnoreCase)))
{
fileList.Add(new FileInfo(newfile.FullName));
newNameList.Add("");
nowState.Add(0);
}
}
}
FileInfo[] files = new DirectoryInfo(i).GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo newfile in files)
{
if (!fileList.Exists(file => file.FullName.Equals(newfile.FullName, StringComparison.OrdinalIgnoreCase)))
{
fileList.Add(new FileInfo(newfile.FullName));
newNameList.Add("");
nowState.Add(0);
}
}
}
}
ShowFileList();
button_rename.Enabled = false;
}
public void ShowFileList()
{
listView1.Items.Clear();
for (int i = 0; i < fileList.Count; i++ )
{
ListViewItem item;
if (checkBox_ShowDir.Checked)
{
item = new ListViewItem(fileList[i].FullName, 0);
item.SubItems.Add(newNameList[i]);
}
else
{
item = new ListViewItem(fileList[i].Name, 0);
item.SubItems.Add(Regex.Replace(newNameList[i],".*\\\\",""));
}
item.SubItems.Add(stateList[nowState[i]]);
listView1.Items.Add(item);
}
toolStripStatusLabel1.Text = "当前有 " + fileList.Count.ToString() + " 个文件(夹)";
}
private void button_clearAll_Click(object sender, EventArgs e)
{
fileList.Clear();
newNameList.Clear();
nowState.Clear();
button_rename.Enabled = false;
ShowFileList();
}
private void button_test_Click(object sender, EventArgs e)
{
string pattern = textBox1.Text;
textBox1.Text = pattern;
string replaceStr = textBox2.Text;
textBox2.Text = replaceStr;
toolStripStatusLabel1.Text = "";
button_rename.Enabled = false;
//测试正则表达式是否合法
if (radioButton1.Checked)
{
try
{
Regex test = new Regex(pattern);
}
catch
{
toolStripStatusLabel1.Text = "输入的正则表达式格式错误,请重新检查";
textBox1.SelectAll();
textBox1.Focus();
return;
}
}
int normalNum = 0;
for (int i = 0; i < fileList.Count; i++)
{
string name = fileList[i].Name;
string extensionName = "";
if (!checkBox_includeExtensionName.Checked)
{
Match matchresult = Regex.Match(name, "^(.*)(\\.[^.]*)$");
if (matchresult.Success)
{
name = matchresult.Groups[1].ToString();
extensionName = matchresult.Groups[2].ToString();
}
}
if (radioButton1.Checked)
{
if (checkBox_IncludePath.Checked)
newNameList[i] = Regex.Replace(fileList[i].Directory + "\\" + name, pattern, replaceStr) + extensionName;
else
newNameList[i] = fileList[i].Directory + "\\" + Regex.Replace(name, pattern, replaceStr) + extensionName;
}
else
{
if (checkBox_IncludePath.Checked)
newNameList[i] = (fileList[i].Directory + "\\" + name).Replace(pattern, replaceStr) + extensionName;
else
newNameList[i] = fileList[i].Directory + "\\" + name.Replace(pattern, replaceStr) + extensionName;
}
newNameList[i] = newNameList[i].Replace("{INDEX}", (i + 1).ToString("D3"));
if (!fileList[i].FullName.Equals(newNameList[i], StringComparison.OrdinalIgnoreCase) && File.Exists(newNameList[i]))
nowState[i] = 3;
else if (fileList[i].IsReadOnly)
nowState[i] = 4;
else if (!Regex.Match(newNameList[i], "^[A-Za-z]:\\\\").Success)
nowState[i] = 5;
else
{
nowState[i] = 0;
normalNum++;
}
}
if (fileList.Count > 0)
button_rename.Enabled = true;
ShowFileList();
if(fileList.Count != normalNum)
toolStripStatusLabel1.Text = "可能会有 " + (fileList.Count - normalNum).ToString() + " 个文件(夹)无法更名";
}
private void button_rename_Click(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "";
int failednum = 0;
for (int i = 0; i < fileList.Count; i++)
{
try
{
string path = Regex.Replace(newNameList[i], "\\\\[^\\\\]*$", "");
if (!Regex.Match(path, "^[A-Za-z]:\\\\").Success)
throw new Exception();
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
fileList[i].MoveTo(newNameList[i]);
nowState[i] = 1;
}
catch
{
nowState[i] = 2;
failednum++;
}
}
ShowFileList();
if(failednum == 0)
toolStripStatusLabel1.Text = "改名成功";
else
toolStripStatusLabel1.Text = "改名失败的文件(夹)个数:" + failednum.ToString() + " 个";
}
private void button_clearSuccessful_Click(object sender, EventArgs e)
{
for (int i = fileList.Count - 1; i >= 0; i--)
{
if (nowState[i] == 1)
{
fileList.RemoveAt(i);
newNameList.RemoveAt(i);
nowState.RemoveAt(i);
}
}
ShowFileList();
}
private void button_clearFailed_Click(object sender, EventArgs e)
{
for (int i = fileList.Count - 1; i >= 0; i--)
{
if (nowState[i] == 2)
{
fileList.RemoveAt(i);
newNameList.RemoveAt(i);
nowState.RemoveAt(i);
}
}
ShowFileList();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
button_rename.Enabled = false;
button_test.Enabled = true;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
button_rename.Enabled = false;
}
private void checkBox_includeExtensionName_CheckedChanged(object sender, EventArgs e)
{
button_rename.Enabled = false;
}
private void checkBox_ShowDir_CheckedChanged(object sender, EventArgs e)
{
ShowFileList();
}
private void checkBox_IncludePath_CheckedChanged(object sender, EventArgs e)
{
if (checkBox_IncludePath.Checked == true)
{
checkBox_ShowDir.Checked = true;
checkBox_ShowDir.Enabled = false;
}
else
{
checkBox_ShowDir.Enabled = true;
}
button_rename.Enabled = false;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
button_rename.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
fileList.Clear();
newNameList.Clear();
nowState.Clear();
button_rename.Enabled = false;
ShowFileList();
textBox1.Text = "";
textBox2.Text = "";
}
}
}