-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainForm.cs
More file actions
626 lines (553 loc) · 19.2 KB
/
Copy pathMainForm.cs
File metadata and controls
626 lines (553 loc) · 19.2 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
using Microsoft.WindowsAPICodePack.Dialogs;
using RenameTools.Src;
using RenameTools.Src.Config;
using RenameTools.Src.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RenameTools
{
public partial class MainForm : Form
{
#if DEBUG
public static bool Debug = false;
#endif
public static bool IsOption = false;
public static NameConfig Config;
static bool Enable = true;
static List<FileFolderInfo> infos;
List<string> NewPathList;
public MainForm()
{
Enable = true;
infos = new List<FileFolderInfo>();
InitializeComponent();
InitConfig();
}
/// <summary>
/// 初始化界面
/// </summary>
private void InitConfig()
{
tbNewNameNumber.Text = "";
tbNewNameNumberLeft.Text = "";
tbNumberBegin.Text = "";
tbNewNameRaw.Text = "";
tbNewNameRawLeft.Text = "";
tbNewSuffix.Text = "";
tbRawField.Text = "";
tbNewField.Text = "";
cbRegularTrue.Checked = false;
cbCaseFalse.Checked = false;
rbRawName.Checked = true;
ClearAllRawFile();
Config_Changed(null,null);
tbNumberBegin.Hint = "0,00,...(默认:0)";
}
public void ClearAllRawFile()
{
infos.Clear();
lbRawNameList.Items.Clear();
lbNewNameList.Items.Clear();
}
/// <summary>
/// 批量重命名面板中点击数字方式或者原名方式切换
/// </summary>
/// <param name="sender">被点击的按钮</param>
/// <param name="e">触发的事件</param>
private void Rb_CheckedChanged(object sender, EventArgs e)
{
if(sender == rbNumberName)
{
// 数字方式命名
ChangNamedWay(true,false);
}
else if(sender == rbRawName)
{
// 原文件名方式命名
ChangNamedWay(false, false);
}
else if (sender == rbNewNameNumberLeft)
{
// 数字方式命名(数字在左)
ChangNamedWay(true, true);
}
else if (sender == rbNewNameRawLeft)
{
// 原文件名方式命名(原文件名在左)
ChangNamedWay(false, true);
}
Config_Changed(sender,e);
}
/// <summary>
/// 改变命名方式
/// </summary>
/// <param name="isNumber">是否是数字</param>
/// <param name="isLeft">是否是左边</param>
void ChangNamedWay(bool isNumber,bool isLeft)
{
tbNewNameNumberLeft.Enabled = isNumber && isLeft;
tbNewNameRawLeft.Enabled = !isNumber && isLeft;
tbNewNameNumber.Enabled = isNumber && !isLeft;
tbNewNameRaw.Enabled = !isNumber && !isLeft;
tbNumberBegin.Enabled = isNumber;
}
private void Btn_Click(object sender, EventArgs e)
{
if(Enable == false)
{
MessageBox.Show("正在执行操作,请稍等!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if(lbUpHelp == sender) {
MessageBox.Show("更改后按Tab键或者点击另外的控件使更改生效。", "帮助");
}
else
if(lbOrderTips == sender)
{
MessageBox.Show("在文件管理器中排好序,选好后点击第一个被选中的项然后拖入列表中即是有序的。","如何排序");
}else if(lbCleanInput == sender)
{
// 点击清除输入按钮
InitConfig();
}
else if (sender == lbSeeChange)
{
// 预览更改
RefreshNewName();
}
else if(btnAddFiles == sender)
{
DialogResult dr = MessageBox.Show("添加文件夹中的所有文件和文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
// 添加文件夹
var commonOpenFileDialog = new CommonOpenFileDialog
{
IsFolderPicker = true, //设置为true为选择文件夹,设置为false为选择文件
Title = "选择需要扫描的文件夹",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
};
var result = commonOpenFileDialog.ShowDialog();
if (result == CommonFileDialogResult.Ok)
{
string path = commonOpenFileDialog.FileName;
if (string.IsNullOrEmpty(path))
{
MessageBox.Show("扫描文件路径不能为空");
}
else
{
//指定的文件夹目录
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists == false)
{
MessageBox.Show("路径不存在!请重新输入");
}
else
{
DialogResult dialogResult = MessageBox.Show("是否添加文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
WorlkFileForm form = new WorlkFileForm();
form.WorlkFilesEvent += Form_WorlkFilesEvent;
form.Start(dir, dialogResult == DialogResult.Yes);
form.ShowDialog();
}
}
}
}
else
{
// 点击添加文件按钮
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件";
dialog.Filter = "所有文件(*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
string[] files = dialog.FileNames;
AddFilesToSourceList(files);
}
}
}
else if(btnStart == sender)
{
// 开始按钮
Enable = false;
ExecuteForm exe = new ExecuteForm(infos);
exe.RenameOkEvent += Exe_ExecuteOkEvent;
exe.ShowDialog(this);
}
else if (sender == lbClean)
{
// 清除所有内容
DialogResult result = MessageBox.Show("是否清除列表中所有数据?", "清除", MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
if(result == DialogResult.OK)
{
ClearAllRawFile();
}
}
}
private void AddListItem(ListBox listBox,object item)
{
if (listBox.InvokeRequired)
{
listBox.Invoke(
new Action<object>(n =>
{
listBox.Items.Add(n);
}
), item);
}
}
private void Form_WorlkFilesEvent(string fileOrDir)
{
string[] filesOrDirs = { fileOrDir };
AddFilesToSourceList(filesOrDirs,true);
}
private void Exe_ExecuteOkEvent(List<string> failure,List<string> NewPath)
{
this.NewPathList = NewPath;
Enable = true;
}
void RefreshNewName()
{
lbRawNameList.Items.Clear();
lbNewNameList.Items.Clear();
for (int i=0;i<infos.Count;i++)
{
infos[i].GetNewName(Config);
lbRawNameList.Items.Add(infos[i]);
lbNewNameList.Items.Add(infos[i].NewName);
}
}
/// <summary>
/// 添加文件到列表中
/// </summary>
/// <param name="array">文件列表</param>
/// <param name="isMt">是否是多線程添加</param>
private void AddFilesToSourceList(Array array, bool isMt = false)
{
if(array == null || array.Length == 0)
{
return;
}
int fail = 0;
foreach (object ff in array)
{
if (File.Exists(ff.ToString())
|| Directory.Exists(ff.ToString()))
{
#if DEBUG
Console.WriteLine("添加文件:" + ff.ToString());
#endif
AddFile(ff.ToString(),isMt);
}else{
fail++;
// Console.WriteLine(ff.ToString());
}
}
if (fail != 0)
{
MessageBox.Show("导入失败" + fail + "个", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LbRawNameList_DragDrop(object sender, DragEventArgs e)
{
object obj = e.Data.GetData(DataFormats.FileDrop);
if (obj is Array array)
{
AddFilesToSourceList(array);
#if DEBUG
Console.WriteLine("文件数组:" + array.Length+"个!");
#endif
}
}
private void LbRawNameList_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// 如果是文件
e.Effect = DragDropEffects.All;
}
else
{
// 如果不是文件
e.Effect = DragDropEffects.None;
}
}
private void LbRawNameList_DoubleClick(object sender, EventArgs e)
{
if(sender!= lbRawNameList)
{
return;
}
int index = lbRawNameList.SelectedIndex;
RemoveFile(index);
}
// 委托
public delegate void NameRuleChange(NameConfig config);
public event NameRuleChange NameRuleChangeEvent;
/// <summary>
/// 添加文件
/// </summary>
/// <param name="file">文件</param>
/// <param name="isMt">是否是多線程添加</param>
public void AddFile(string file,bool isMt = false)
{
if (IsOption)
{
return;
}
IsOption = true;
FileFolderInfo info = new FileFolderInfo(this,file, infos.Count);
infos.Add(info);
if (isMt)
{
AddListItem(lbNewNameList, info.GetNewName(Config));
AddListItem(lbRawNameList, info);
}
else
{
lbNewNameList.Items.Add(info.GetNewName(Config));
lbRawNameList.Items.Add(info);
}
#if DEBUG
if (Debug)
{
Console.WriteLine("添加文件");
}
#endif
IsOption = false;
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="index">文件索引</param>
public void RemoveFile(int index)
{
if(IsOption)
{
return;
}
IsOption = true;
if (index < infos.Count && index >= 0)
{
infos.RemoveAt(index);
lbNewNameList.Items.RemoveAt(index);
lbRawNameList.Items.RemoveAt(index);
for (int i = index;i< infos.Count; i++)
{
infos[i].SetIndex(i);
lbNewNameList.Items[i] = infos[i].ToString(true, true);
lbRawNameList.Items[i] = infos[i].ToString();
}
#if DEBUG
if (Debug)
{
Console.WriteLine("移除文件");
}
#endif
}
IsOption = false;
}
/// <summary>
/// 只允许输入0-9的字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InputNumber_KeyPress(object sender, KeyPressEventArgs e)
{
//这是允许输入退格键
if (e.KeyChar != '\b')
{
//这是允许输入0-9数字
if ((e.KeyChar < '0') || (e.KeyChar > '9'))
{
e.Handled = true;
}
}
}
/// <summary>
/// 文件名合法输入校验
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InputFileName_KeyPress(object sender, KeyPressEventArgs e)
{
// 文件名不能包含如下字符
if (
e.KeyChar == '\\'||
e.KeyChar == '/' ||
e.KeyChar == ':' ||
e.KeyChar == '*' ||
e.KeyChar == '?' ||
e.KeyChar == '"' ||
e.KeyChar == '<' ||
e.KeyChar == '>' ||
e.KeyChar == '|'
)
{
e.Handled = true;
}
}
/// <summary>
/// 忽略大小写和正则的改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cb_CheckedChanged(object sender, EventArgs e)
{
if(sender == cbCaseFalse)
{
if (cbCaseFalse.Checked)
{
cbRegularTrue.Checked = false;
}
}
else if (sender == cbRegularTrue)
{
if (cbRegularTrue.Checked)
{
cbCaseFalse.Checked = false;
}
}
Config_Changed(sender,e);
}
private void Config_Changed(object sender, EventArgs e)
{
if(Config == null)
{
Config = new NameConfig();
}
bool isChange = false;
if (Config.caseTrue != cbCaseFalse.Checked)
{
isChange = true;
Config.caseTrue = cbCaseFalse.Checked;
}
if (Config.regularTrue != cbRegularTrue.Checked)
{
isChange = true;
Config.regularTrue = cbRegularTrue.Checked;
}
if (Config.findText != tbRawField.Text)
{
isChange = true;
Config.findText = tbRawField.Text;
}
if (Config.targetText != tbNewField.Text)
{
isChange = true;
Config.targetText = tbNewField.Text;
}
if (Config.extend != tbNewSuffix.Text)
{
isChange = true;
Config.extend = tbNewSuffix.Text;
}
RenameMode mode = RenameMode.None;
string p1 = null, p2 = null;
if (rbNumberName.Checked)
{
// *** + 数字
mode = RenameMode.TextNumber;
p1 = tbNewNameNumber.Text;
if (tbNumberBegin.IsHint)
{
p2 = "0";
}
else
{
p2 = tbNumberBegin.Text;
}
}
else if (rbNewNameNumberLeft.Checked)
{
// 数字 + ***
mode = RenameMode.NumberText;
p1 = tbNewNameNumberLeft.Text;
if (tbNumberBegin.IsHint)
{
p2 = "0";
}
else
{
p2 = tbNumberBegin.Text;
}
}
else if (rbRawName.Checked)
{
// *** + 原名
mode = RenameMode.TextName;
p1 = tbNewNameRaw.Text;
}
else if (rbNewNameRawLeft.Checked)
{
// 原名 + ***
mode = RenameMode.NameText;
p1 = tbNewNameRawLeft.Text;
}
if ((Config.mode != mode) && (mode != RenameMode.None))
{
isChange = true;
Config.mode = mode;
}
if (Config.p1 != p1)
{
isChange = true;
Config.p1 = p1;
}
if (Config.p2 != p2)
{
isChange = true;
Config.p2 = p2;
}
if (isChange)
{
#if DEBUG
Console.WriteLine("Mode : " + Config.mode);
#endif
NameRuleChangeEvent?.Invoke(Config);
RefreshNewName();
}
}
private void List_SelectedIndexChanged(object sender, EventArgs e)
{
if(sender is ListBox lb)
{
if(lb.SelectedIndex >=0 && lb.SelectedIndex< lb.Items.Count) {
tbName.Text = lb.Items[lb.SelectedIndex].ToString();
}
}
}
private void TextBox_Leave(object sender, EventArgs e)
{
Config_Changed(sender,e);
}
/// <summary>
/// 重新引入文件,用于改名后重新添加到待改名位置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Reload_Click(object sender, EventArgs e)
{
if(this.NewPathList == null)
{
MessageBox.Show("無需重新引入","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
// 清除所有內容
ClearAllRawFile();
AddFilesToSourceList(this.NewPathList.ToArray());
this.NewPathList = null;
}
}
}