-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuAdvancedFileManagerForm.pas
More file actions
982 lines (844 loc) · 25.2 KB
/
uAdvancedFileManagerForm.pas
File metadata and controls
982 lines (844 loc) · 25.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
unit uAdvancedFileManagerForm;
{
高级文件管理界面 - Phase 2.1
功能包括:
- 文件搜索界面
- 重复文件查找界面
- 大文件分析界面
- 批量操作界面
- 文件分类展示
作者: AI助手
版本: 2.1.0
日期: 2024
}
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
System.Generics.Collections, System.IOUtils, System.Threading,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
Vcl.ComCtrls, Vcl.Buttons, Vcl.CheckLst, Vcl.Menus,
uAdvancedFileManager;
type
TfrmAdvancedFileManager = class(TForm)
// 主面板
pnlMain: TPanel;
// 标签页控制
pgcMain: TPageControl;
// 文件搜索页
tsFileSearch: TTabSheet;
pnlSearchCriteria: TPanel;
lblSearchPath: TLabel;
edtSearchPath: TEdit;
btnBrowseSearchPath: TBitBtn;
lblNamePattern: TLabel;
edtNamePattern: TEdit;
chkUseRegex: TCheckBox;
lblSizeRange: TLabel;
edtMinSize: TEdit;
lblSizeTo: TLabel;
edtMaxSize: TEdit;
cmbSizeUnit: TComboBox;
lblDateRange: TLabel;
dtpDateFrom: TDateTimePicker;
lblDateTo: TLabel;
dtpDateTo: TDateTimePicker;
chkIncludeSubdirs: TCheckBox;
chkIncludeHidden: TCheckBox;
chkIncludeSystem: TCheckBox;
btnStartSearch: TBitBtn;
btnCancelSearch: TBitBtn;
lvSearchResults: TListView;
// 重复文件页
tsDuplicateFiles: TTabSheet;
pnlDuplicateControls: TPanel;
lblDuplicatePath: TLabel;
edtDuplicatePath: TEdit;
btnBrowseDuplicatePath: TBitBtn;
btnFindDuplicates: TBitBtn;
btnCancelDuplicate: TBitBtn;
tvDuplicateGroups: TTreeView;
pnlDuplicateInfo: TPanel;
lblDuplicateInfo: TLabel;
btnDeleteSelected: TBitBtn;
btnKeepNewest: TBitBtn;
btnKeepLargest: TBitBtn;
// 大文件页
tsLargeFiles: TTabSheet;
pnlLargeFileControls: TPanel;
lblLargePath: TLabel;
edtLargePath: TEdit;
btnBrowseLargePath: TBitBtn;
lblMinFileSize: TLabel;
edtMinFileSize: TEdit;
cmbFileSizeUnit: TComboBox;
btnFindLargeFiles: TBitBtn;
btnCancelLarge: TBitBtn;
lvLargeFiles: TListView;
// 批量操作页
tsBatchOp: TTabSheet;
pnlBatchControls: TPanel;
lblBatchOperation: TLabel;
cmbBatchOperation: TComboBox;
lblTargetPath: TLabel;
edtTargetPath: TEdit;
btnBrowseTargetPath: TBitBtn;
chkOverwrite: TCheckBox;
btnAddFiles: TBitBtn;
btnRemoveFiles: TBitBtn;
btnClearFiles: TBitBtn;
btnExecuteBatch: TBitBtn;
lvBatchFiles: TListView;
// 状态栏和进度
pnlBottom: TPanel;
lblStatus: TLabel;
ProgressBar: TProgressBar;
btnCancel: TBitBtn;
// 右键菜单
pmFileList: TPopupMenu;
miOpenFile: TMenuItem;
miOpenFolder: TMenuItem;
miCopyPath: TMenuItem;
miSeparator1: TMenuItem;
miProperties: TMenuItem;
miDelete: TMenuItem;
// 事件处理
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
// 搜索功能
procedure btnBrowseSearchPathClick(Sender: TObject);
procedure btnStartSearchClick(Sender: TObject);
procedure btnCancelSearchClick(Sender: TObject);
procedure lvSearchResultsDblClick(Sender: TObject);
// 重复文件功能
procedure btnBrowseDuplicatePathClick(Sender: TObject);
procedure btnFindDuplicatesClick(Sender: TObject);
procedure btnCancelDuplicateClick(Sender: TObject);
procedure btnDeleteSelectedClick(Sender: TObject);
procedure btnKeepNewestClick(Sender: TObject);
procedure btnKeepLargestClick(Sender: TObject);
procedure tvDuplicateGroupsChange(Sender: TObject; Node: TTreeNode);
// 大文件功能
procedure btnBrowseLargePathClick(Sender: TObject);
procedure btnFindLargeFilesClick(Sender: TObject);
procedure btnCancelLargeClick(Sender: TObject);
procedure lvLargeFilesDblClick(Sender: TObject);
// 批量操作功能
procedure btnBrowseTargetPathClick(Sender: TObject);
procedure btnAddFilesClick(Sender: TObject);
procedure btnRemoveFilesClick(Sender: TObject);
procedure btnClearFilesClick(Sender: TObject);
procedure btnExecuteBatchClick(Sender: TObject);
procedure cmbBatchOperationChange(Sender: TObject);
// 右键菜单
procedure miOpenFileClick(Sender: TObject);
procedure miOpenFolderClick(Sender: TObject);
procedure miCopyPathClick(Sender: TObject);
procedure miPropertiesClick(Sender: TObject);
procedure miDeleteClick(Sender: TObject);
// 通用操作
procedure btnCancelClick(Sender: TObject);
private
FFileManager: TAdvancedFileManager;
FCurrentTask: TThread;
FDuplicateGroups: TArray<TDuplicateFileGroup>;
FLargeFiles: TArray<TAdvancedFileInfo>;
FSearchResults: TArray<TAdvancedFileInfo>;
procedure InitializeInterface;
procedure UpdateStatus(const Message: string; Progress: Integer);
procedure OnFileManagerProgress(const Message: string; Progress: Integer; Cancel: Boolean);
// 界面更新方法
procedure PopulateSearchResults(const Results: TArray<TAdvancedFileInfo>);
procedure PopulateDuplicateGroups(const Groups: TArray<TDuplicateFileGroup>);
procedure PopulateLargeFiles(const Files: TArray<TAdvancedFileInfo>);
procedure UpdateDuplicateInfo(const Group: TDuplicateFileGroup);
// 工具方法
function GetCurrentListView: TListView;
function GetSelectedFiles: TArray<string>;
function ParseFileSize(const SizeText: string; SizeUnit: Integer): Int64;
function FormatFileInfo(const FileInfo: TAdvancedFileInfo): string;
// 批量操作相关
procedure UpdateBatchControls;
function GetBatchOperationParams: TBatchOperationParams;
public
{ Public declarations }
end;
var
frmAdvancedFileManager: TfrmAdvancedFileManager;
implementation
{$R *.dfm}
uses
System.UITypes, Vcl.FileCtrl, Vcl.Clipbrd, Winapi.ShellAPI;
{ TfrmAdvancedFileManager }
procedure TfrmAdvancedFileManager.FormCreate(Sender: TObject);
begin
FFileManager := TAdvancedFileManager.Create;
FFileManager.OnProgress := OnFileManagerProgress;
InitializeInterface;
end;
procedure TfrmAdvancedFileManager.FormDestroy(Sender: TObject);
begin
if Assigned(FCurrentTask) then
begin
FFileManager.CancelOperation;
if not FCurrentTask.Finished then
begin
FCurrentTask.WaitFor;
end;
end;
FFileManager.Free;
end;
procedure TfrmAdvancedFileManager.FormShow(Sender: TObject);
begin
// 设置默认搜索路径为C盘用户目录
edtSearchPath.Text := GetEnvironmentVariable('USERPROFILE');
edtDuplicatePath.Text := edtSearchPath.Text;
edtLargePath.Text := edtSearchPath.Text;
// 设置默认值
cmbSizeUnit.ItemIndex := 2; // MB
cmbFileSizeUnit.ItemIndex := 2; // MB
edtMinFileSize.Text := '100'; // 100MB
// 设置日期范围
dtpDateFrom.Date := Now - 30; // 30天前
dtpDateTo.Date := Now;
UpdateStatus('就绪', 0);
end;
procedure TfrmAdvancedFileManager.InitializeInterface;
begin
// 设置界面
Caption := '高级文件管理器';
Position := poScreenCenter;
WindowState := wsMaximized;
// 设置标签页
pgcMain.ActivePageIndex := 0;
// 初始化组合框
cmbSizeUnit.Items.Clear;
cmbSizeUnit.Items.AddStrings(['B', 'KB', 'MB', 'GB']);
cmbFileSizeUnit.Items.Clear;
cmbFileSizeUnit.Items.AddStrings(['B', 'KB', 'MB', 'GB']);
cmbBatchOperation.Items.Clear;
cmbBatchOperation.Items.AddStrings(['复制文件', '移动文件', '删除文件', '重命名文件']);
cmbBatchOperation.ItemIndex := 0;
// 设置ListView
with lvSearchResults do
begin
ViewStyle := vsReport;
RowSelect := True;
GridLines := True;
Columns.Clear;
with Columns.Add do
begin
Caption := '文件名';
Width := 200;
end;
with Columns.Add do
begin
Caption := '路径';
Width := 300;
end;
with Columns.Add do
begin
Caption := '大小';
Width := 100;
end;
with Columns.Add do
begin
Caption := '修改时间';
Width := 150;
end;
with Columns.Add do
begin
Caption := '类型';
Width := 100;
end;
end;
// 设置大文件ListView
with lvLargeFiles do
begin
ViewStyle := vsReport;
RowSelect := True;
GridLines := True;
Columns.Clear;
with Columns.Add do
begin
Caption := '文件名';
Width := 200;
end;
with Columns.Add do
begin
Caption := '路径';
Width := 300;
end;
with Columns.Add do
begin
Caption := '大小';
Width := 100;
end;
with Columns.Add do
begin
Caption := '修改时间';
Width := 150;
end;
end;
// 设置批量操作ListView
with lvBatchFiles do
begin
ViewStyle := vsReport;
RowSelect := True;
GridLines := True;
CheckBoxes := True;
Columns.Clear;
with Columns.Add do
begin
Caption := '文件名';
Width := 200;
end;
with Columns.Add do
begin
Caption := '路径';
Width := 400;
end;
with Columns.Add do
begin
Caption := '大小';
Width := 100;
end;
end;
// 设置重复文件TreeView
tvDuplicateGroups.ReadOnly := True;
tvDuplicateGroups.ShowLines := True;
tvDuplicateGroups.ShowButtons := True;
UpdateBatchControls;
end;
procedure TfrmAdvancedFileManager.UpdateStatus(const Message: string; Progress: Integer);
begin
lblStatus.Caption := Message;
if Progress >= 0 then
begin
ProgressBar.Visible := True;
ProgressBar.Position := Progress;
end
else
begin
ProgressBar.Visible := False;
end;
Application.ProcessMessages;
end;
procedure TfrmAdvancedFileManager.OnFileManagerProgress(const Message: string;
Progress: Integer; Cancel: Boolean);
begin
// 在主线程中更新界面
TThread.Queue(nil,
procedure
begin
UpdateStatus(Message, Progress);
btnCancel.Enabled := not Cancel;
end);
end;
// 文件搜索功能实现
procedure TfrmAdvancedFileManager.btnBrowseSearchPathClick(Sender: TObject);
var
Dir: string;
begin
Dir := edtSearchPath.Text;
if SelectDirectory('选择搜索目录', '', Dir) then
edtSearchPath.Text := Dir;
end;
procedure TfrmAdvancedFileManager.btnStartSearchClick(Sender: TObject);
var
Criteria: TFileSearchCriteria;
begin
if Trim(edtSearchPath.Text) = '' then
begin
ShowMessage('请选择搜索路径!');
Exit;
end;
// 设置搜索条件
ZeroMemory(@Criteria, SizeOf(Criteria));
Criteria.SearchPath := Trim(edtSearchPath.Text);
Criteria.NamePattern := Trim(edtNamePattern.Text);
Criteria.UseRegex := chkUseRegex.Checked;
Criteria.IncludeSubdirs := chkIncludeSubdirs.Checked;
Criteria.IncludeHidden := chkIncludeHidden.Checked;
Criteria.IncludeSystem := chkIncludeSystem.Checked;
// 解析大小范围
if Trim(edtMinSize.Text) <> '' then
Criteria.MinSize := ParseFileSize(edtMinSize.Text, cmbSizeUnit.ItemIndex);
if Trim(edtMaxSize.Text) <> '' then
Criteria.MaxSize := ParseFileSize(edtMaxSize.Text, cmbSizeUnit.ItemIndex);
// 设置日期范围
Criteria.DateFrom := dtpDateFrom.Date;
Criteria.DateTo := dtpDateTo.Date;
btnStartSearch.Enabled := False;
btnCancelSearch.Enabled := True;
FCurrentTask := TThread.CreateAnonymousThread(
procedure
var
Results: TArray<TAdvancedFileInfo>;
begin
try
Results := FFileManager.SearchFiles(Criteria);
TThread.Queue(nil,
procedure
begin
FSearchResults := Results;
PopulateSearchResults(Results);
btnStartSearch.Enabled := True;
btnCancelSearch.Enabled := False;
end);
except
on E: Exception do
TThread.Queue(nil,
procedure
begin
UpdateStatus('搜索失败: ' + E.Message, -1);
btnStartSearch.Enabled := True;
btnCancelSearch.Enabled := False;
end);
end;
end);
FCurrentTask.Start;
end;
procedure TfrmAdvancedFileManager.btnCancelSearchClick(Sender: TObject);
begin
FFileManager.CancelOperation;
btnCancelSearch.Enabled := False;
end;
procedure TfrmAdvancedFileManager.PopulateSearchResults(const Results: TArray<TAdvancedFileInfo>);
var
I: Integer;
Item: TListItem;
begin
lvSearchResults.Items.BeginUpdate;
try
lvSearchResults.Items.Clear;
for I := 0 to High(Results) do
begin
Item := lvSearchResults.Items.Add;
Item.Caption := Results[I].FileName;
Item.SubItems.Add(ExtractFilePath(Results[I].FullPath));
Item.SubItems.Add(FFileManager.FormatFileSize(Results[I].FileSize));
Item.SubItems.Add(DateTimeToStr(Results[I].LastWriteTime));
Item.SubItems.Add(Results[I].Category);
Item.Data := Pointer(I);
end;
UpdateStatus(Format('搜索完成,找到 %d 个文件', [Length(Results)]), 100);
finally
lvSearchResults.Items.EndUpdate;
end;
end;
// 重复文件功能实现
procedure TfrmAdvancedFileManager.btnBrowseDuplicatePathClick(Sender: TObject);
var
Dir: string;
begin
Dir := edtDuplicatePath.Text;
if SelectDirectory('选择扫描目录', '', Dir) then
edtDuplicatePath.Text := Dir;
end;
procedure TfrmAdvancedFileManager.btnFindDuplicatesClick(Sender: TObject);
begin
if Trim(edtDuplicatePath.Text) = '' then
begin
ShowMessage('请选择扫描路径!');
Exit;
end;
btnFindDuplicates.Enabled := False;
btnCancelDuplicate.Enabled := True;
FCurrentTask := TThread.CreateAnonymousThread(
procedure
var
Groups: TArray<TDuplicateFileGroup>;
begin
try
Groups := FFileManager.FindDuplicateFiles(edtDuplicatePath.Text);
TThread.Queue(nil,
procedure
begin
FDuplicateGroups := Groups;
PopulateDuplicateGroups(Groups);
btnFindDuplicates.Enabled := True;
btnCancelDuplicate.Enabled := False;
end);
except
on E: Exception do
TThread.Queue(nil,
procedure
begin
UpdateStatus('扫描失败: ' + E.Message, -1);
btnFindDuplicates.Enabled := True;
btnCancelDuplicate.Enabled := False;
end);
end;
end);
FCurrentTask.Start;
end;
procedure TfrmAdvancedFileManager.PopulateDuplicateGroups(const Groups: TArray<TDuplicateFileGroup>);
var
I, J: Integer;
GroupNode, FileNode: TTreeNode;
Group: TDuplicateFileGroup;
TotalWastedSpace: Int64;
begin
tvDuplicateGroups.Items.BeginUpdate;
try
tvDuplicateGroups.Items.Clear;
TotalWastedSpace := 0;
for I := 0 to High(Groups) do
begin
Group := Groups[I];
GroupNode := tvDuplicateGroups.Items.Add(nil,
Format('重复组 %d - %d 个文件 - %s',
[I + 1, Group.FileCount, FFileManager.FormatFileSize(Group.TotalSize)]));
GroupNode.Data := Pointer(I);
for J := 0 to High(Group.Files) do
begin
FileNode := tvDuplicateGroups.Items.AddChild(GroupNode,
Format('%s (%s)', [Group.Files[J].FileName,
FFileManager.FormatFileSize(Group.Files[J].FileSize)]));
FileNode.Data := Pointer(J);
end;
// 计算浪费的空间(除了一个文件,其他都是重复的)
TotalWastedSpace := TotalWastedSpace + (Group.TotalSize - Group.Files[0].FileSize);
end;
UpdateStatus(Format('找到 %d 组重复文件,可节省空间 %s',
[Length(Groups), FFileManager.FormatFileSize(TotalWastedSpace)]), 100);
finally
tvDuplicateGroups.Items.EndUpdate;
end;
end;
// 批量操作功能实现
procedure TfrmAdvancedFileManager.btnAddFilesClick(Sender: TObject);
var
OpenDialog: TOpenDialog;
I: Integer;
Item: TListItem;
FileInfo: TAdvancedFileInfo;
begin
OpenDialog := TOpenDialog.Create(Self);
try
OpenDialog.Options := [ofAllowMultiSelect, ofFileMustExist];
OpenDialog.Title := '选择要添加的文件';
if OpenDialog.Execute then
begin
lvBatchFiles.Items.BeginUpdate;
try
for I := 0 to OpenDialog.Files.Count - 1 do
begin
FileInfo := FFileManager.GetFileInfo(OpenDialog.Files[I]);
Item := lvBatchFiles.Items.Add;
Item.Caption := FileInfo.FileName;
Item.SubItems.Add(FileInfo.FullPath);
Item.SubItems.Add(FFileManager.FormatFileSize(FileInfo.FileSize));
Item.Checked := True;
end;
finally
lvBatchFiles.Items.EndUpdate;
end;
UpdateStatus(Format('已添加 %d 个文件到批量操作列表', [OpenDialog.Files.Count]), -1);
end;
finally
OpenDialog.Free;
end;
end;
procedure TfrmAdvancedFileManager.btnExecuteBatchClick(Sender: TObject);
var
Files: TArray<string>;
Params: TBatchOperationParams;
I: Integer;
FileList: TStringList;
begin
// 收集选中的文件
FileList := TStringList.Create;
try
for I := 0 to lvBatchFiles.Items.Count - 1 do
begin
if lvBatchFiles.Items[I].Checked then
FileList.Add(lvBatchFiles.Items[I].SubItems[0]); // 完整路径
end;
if FileList.Count = 0 then
begin
ShowMessage('请选择要操作的文件!');
Exit;
end;
Files := FileList.ToStringArray;
finally
FileList.Free;
end;
// 获取操作参数
Params := GetBatchOperationParams;
btnExecuteBatch.Enabled := False;
FCurrentTask := TThread.CreateAnonymousThread(
procedure
var
Success: Boolean;
begin
try
Success := FFileManager.BatchOperation(Files, Params);
TThread.Queue(nil,
procedure
begin
if Success then
UpdateStatus('批量操作完成', 100)
else
UpdateStatus('批量操作失败', -1);
btnExecuteBatch.Enabled := True;
end);
except
on E: Exception do
TThread.Queue(nil,
procedure
begin
UpdateStatus('批量操作异常: ' + E.Message, -1);
btnExecuteBatch.Enabled := True;
end);
end;
end);
FCurrentTask.Start;
end;
function TfrmAdvancedFileManager.GetBatchOperationParams: TBatchOperationParams;
begin
ZeroMemory(@Result, SizeOf(Result));
case cmbBatchOperation.ItemIndex of
0: Result.OperationType := botCopy;
1: Result.OperationType := botMove;
2: Result.OperationType := botDelete;
3: Result.OperationType := botRename;
else
Result.OperationType := botNone;
end;
Result.TargetPath := Trim(edtTargetPath.Text);
Result.OverwriteExisting := chkOverwrite.Checked;
end;
// 工具方法实现
function TfrmAdvancedFileManager.ParseFileSize(const SizeText: string; SizeUnit: Integer): Int64;
var
Size: Double;
begin
try
Size := StrToFloat(SizeText);
case SizeUnit of
0: Result := Round(Size); // Bytes
1: Result := Round(Size * 1024); // KB
2: Result := Round(Size * 1024 * 1024); // MB
3: Result := Round(Size * 1024 * 1024 * 1024); // GB
else
Result := Round(Size);
end;
except
Result := 0;
end;
end;
procedure TfrmAdvancedFileManager.UpdateBatchControls;
begin
case cmbBatchOperation.ItemIndex of
0, 1: // 复制、移动
begin
lblTargetPath.Visible := True;
edtTargetPath.Visible := True;
btnBrowseTargetPath.Visible := True;
chkOverwrite.Visible := True;
end;
2: // 删除
begin
lblTargetPath.Visible := False;
edtTargetPath.Visible := False;
btnBrowseTargetPath.Visible := False;
chkOverwrite.Visible := False;
end;
3: // 重命名
begin
lblTargetPath.Visible := False;
edtTargetPath.Visible := False;
btnBrowseTargetPath.Visible := False;
chkOverwrite.Visible := True;
end;
end;
end;
// 事件处理方法
procedure TfrmAdvancedFileManager.cmbBatchOperationChange(Sender: TObject);
begin
UpdateBatchControls;
end;
procedure TfrmAdvancedFileManager.btnBrowseTargetPathClick(Sender: TObject);
var
Dir: string;
begin
Dir := edtTargetPath.Text;
if SelectDirectory('选择目标目录', '', Dir) then
edtTargetPath.Text := Dir;
end;
procedure TfrmAdvancedFileManager.btnRemoveFilesClick(Sender: TObject);
var
I: Integer;
begin
for I := lvBatchFiles.Items.Count - 1 downto 0 do
begin
if lvBatchFiles.Items[I].Selected then
lvBatchFiles.Items.Delete(I);
end;
end;
procedure TfrmAdvancedFileManager.btnClearFilesClick(Sender: TObject);
begin
lvBatchFiles.Items.Clear;
end;
procedure TfrmAdvancedFileManager.btnCancelClick(Sender: TObject);
begin
FFileManager.CancelOperation;
end;
// 其他未实现的事件处理方法的占位符
procedure TfrmAdvancedFileManager.lvSearchResultsDblClick(Sender: TObject);
begin
// TODO: 实现双击打开文件
end;
procedure TfrmAdvancedFileManager.btnCancelDuplicateClick(Sender: TObject);
begin
FFileManager.CancelOperation;
btnCancelDuplicate.Enabled := False;
end;
procedure TfrmAdvancedFileManager.btnDeleteSelectedClick(Sender: TObject);
begin
// TODO: 实现删除选中的重复文件
end;
procedure TfrmAdvancedFileManager.btnKeepNewestClick(Sender: TObject);
begin
// TODO: 实现保留最新文件,删除其他重复文件
end;
procedure TfrmAdvancedFileManager.btnKeepLargestClick(Sender: TObject);
begin
// TODO: 实现保留最大文件,删除其他重复文件
end;
procedure TfrmAdvancedFileManager.tvDuplicateGroupsChange(Sender: TObject; Node: TTreeNode);
begin
// TODO: 实现重复文件组信息显示
end;
procedure TfrmAdvancedFileManager.btnBrowseLargePathClick(Sender: TObject);
var
Dir: string;
begin
Dir := edtLargePath.Text;
if SelectDirectory('选择扫描目录', '', Dir) then
edtLargePath.Text := Dir;
end;
procedure TfrmAdvancedFileManager.btnFindLargeFilesClick(Sender: TObject);
var
MinSize: Int64;
begin
if Trim(edtLargePath.Text) = '' then
begin
ShowMessage('请选择扫描路径!');
Exit;
end;
MinSize := ParseFileSize(edtMinFileSize.Text, cmbFileSizeUnit.ItemIndex);
if MinSize <= 0 then
begin
ShowMessage('请输入有效的最小文件大小!');
Exit;
end;
btnFindLargeFiles.Enabled := False;
btnCancelLarge.Enabled := True;
FCurrentTask := TThread.CreateAnonymousThread(
procedure
var
Files: TArray<TAdvancedFileInfo>;
begin
try
Files := FFileManager.FindLargeFiles(edtLargePath.Text, MinSize);
TThread.Queue(nil,
procedure
begin
FLargeFiles := Files;
PopulateLargeFiles(Files);
btnFindLargeFiles.Enabled := True;
btnCancelLarge.Enabled := False;
end);
except
on E: Exception do
TThread.Queue(nil,
procedure
begin
UpdateStatus('扫描失败: ' + E.Message, -1);
btnFindLargeFiles.Enabled := True;
btnCancelLarge.Enabled := False;
end);
end;
end);
FCurrentTask.Start;
end;
procedure TfrmAdvancedFileManager.PopulateLargeFiles(const Files: TArray<TAdvancedFileInfo>);
var
I: Integer;
Item: TListItem;
begin
lvLargeFiles.Items.BeginUpdate;
try
lvLargeFiles.Items.Clear;
for I := 0 to High(Files) do
begin
Item := lvLargeFiles.Items.Add;
Item.Caption := Files[I].FileName;
Item.SubItems.Add(ExtractFilePath(Files[I].FullPath));
Item.SubItems.Add(FFileManager.FormatFileSize(Files[I].FileSize));
Item.SubItems.Add(DateTimeToStr(Files[I].LastWriteTime));
Item.Data := Pointer(I);
end;
UpdateStatus(Format('找到 %d 个大文件', [Length(Files)]), 100);
finally
lvLargeFiles.Items.EndUpdate;
end;
end;
procedure TfrmAdvancedFileManager.btnCancelLargeClick(Sender: TObject);
begin
FFileManager.CancelOperation;
btnCancelLarge.Enabled := False;
end;
procedure TfrmAdvancedFileManager.lvLargeFilesDblClick(Sender: TObject);
begin
// TODO: 实现双击打开大文件
end;
// 右键菜单事件处理方法
procedure TfrmAdvancedFileManager.miOpenFileClick(Sender: TObject);
begin
// TODO: 实现打开文件
end;
procedure TfrmAdvancedFileManager.miOpenFolderClick(Sender: TObject);
begin
// TODO: 实现打开文件夹
end;
procedure TfrmAdvancedFileManager.miCopyPathClick(Sender: TObject);
begin
// TODO: 实现复制路径到剪贴板
end;
procedure TfrmAdvancedFileManager.miPropertiesClick(Sender: TObject);
begin
// TODO: 实现显示文件属性
end;
procedure TfrmAdvancedFileManager.miDeleteClick(Sender: TObject);
begin
// TODO: 实现删除文件
end;
// 工具方法的占位符实现
function TfrmAdvancedFileManager.GetCurrentListView: TListView;
begin
case pgcMain.ActivePageIndex of
0: Result := lvSearchResults;
2: Result := lvLargeFiles;
3: Result := lvBatchFiles;
else
Result := nil;
end;
end;
function TfrmAdvancedFileManager.GetSelectedFiles: TArray<string>;
begin
// TODO: 实现获取当前选中的文件列表
SetLength(Result, 0);
end;
function TfrmAdvancedFileManager.FormatFileInfo(const FileInfo: TAdvancedFileInfo): string;
begin
Result := Format('%s (%s)', [FileInfo.FileName, FFileManager.FormatFileSize(FileInfo.FileSize)]);
end;
procedure TfrmAdvancedFileManager.UpdateDuplicateInfo(const Group: TDuplicateFileGroup);
begin
// TODO: 实现重复文件组详细信息显示
end;
end.