-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuCleanupRulesPreview.pas
More file actions
362 lines (325 loc) · 9.81 KB
/
uCleanupRulesPreview.pas
File metadata and controls
362 lines (325 loc) · 9.81 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
unit uCleanupRulesPreview;
interface
uses
System.SysUtils, System.Classes, System.Generics.Collections,
System.IOUtils, System.Types, System.DateUtils,
Vcl.Forms, Vcl.Controls, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.Dialogs;
type
TfrmCleanupRulesPreview = class(TForm)
private
pnlTop: TPanel;
lblRoot: TLabel;
edtRoot: TEdit;
btnBrowse: TButton;
lblPatterns: TLabel;
memPatterns: TMemo; // 包含模式,glob-like: *.log, *.tmp
lblWhitelist: TLabel;
memWhitelist: TMemo; // 白名单目录(逐行)
lblMinDays: TLabel;
edtMinDays: TEdit; // 最小修改天数
btnPreview: TButton;
btnExportCSV: TButton;
lvResult: TListView;
lblHint: TLabel;
procedure FormCreate(Sender: TObject);
procedure BtnBrowseClick(Sender: TObject);
procedure BtnPreviewClick(Sender: TObject);
procedure BtnExportCSVClick(Sender: TObject);
function MatchAnyPattern(const FileName: string; const Patterns: TArray<string>): Boolean;
function IsInWhitelist(const FilePath: string; const Whitelist: TArray<string>): Boolean;
function ShouldInclude(const FilePath: string; const LastWrite: TDateTime;
const Patterns, White: TArray<string>; const MinDays: Integer): Boolean;
procedure ExportListViewToCSV(LV: TListView; const FileName: string);
public
class procedure ShowPreview; static;
end;
implementation
class procedure TfrmCleanupRulesPreview.ShowPreview;
var
F: TfrmCleanupRulesPreview;
begin
F := TfrmCleanupRulesPreview.Create(nil);
try
F.Position := poScreenCenter;
F.ShowModal;
finally
F.Free;
end;
end;
procedure TfrmCleanupRulesPreview.FormCreate(Sender: TObject);
begin
Caption := '清理规则预览(只读)';
Width := 900;
Height := 620;
pnlTop := TPanel.Create(Self);
pnlTop.Parent := Self;
pnlTop.Align := alTop;
pnlTop.Height := 180;
lblRoot := TLabel.Create(Self);
lblRoot.Parent := pnlTop;
lblRoot.Left := 8;
lblRoot.Top := 12;
lblRoot.Caption := '根目录:';
edtRoot := TEdit.Create(Self);
edtRoot.Parent := pnlTop;
edtRoot.Left := 64;
edtRoot.Top := 8;
edtRoot.Width := 520;
edtRoot.Text := 'C:\\Users\\';
btnBrowse := TButton.Create(Self);
btnBrowse.Parent := pnlTop;
btnBrowse.Left := edtRoot.Left + edtRoot.Width + 8;
btnBrowse.Top := 6;
btnBrowse.Width := 80;
btnBrowse.Caption := '浏览...';
btnBrowse.OnClick := BtnBrowseClick;
lblPatterns := TLabel.Create(Self);
lblPatterns.Parent := pnlTop;
lblPatterns.Left := 8;
lblPatterns.Top := 44;
lblPatterns.Caption := '包含模式(每行一个,如 *.log):';
memPatterns := TMemo.Create(Self);
memPatterns.Parent := pnlTop;
memPatterns.Left := 8;
memPatterns.Top := 64;
memPatterns.Width := 300;
memPatterns.Height := 88;
memPatterns.ScrollBars := ssVertical;
memPatterns.Lines.Text := '*.log' + sLineBreak + '*.tmp';
lblWhitelist := TLabel.Create(Self);
lblWhitelist.Parent := pnlTop;
lblWhitelist.Left := memPatterns.Left + memPatterns.Width + 8;
lblWhitelist.Top := 44;
lblWhitelist.Caption := '白名单目录(不清理,每行一个):';
memWhitelist := TMemo.Create(Self);
memWhitelist.Parent := pnlTop;
memWhitelist.Left := lblWhitelist.Left;
memWhitelist.Top := 64;
memWhitelist.Width := 300;
memWhitelist.Height := 88;
memWhitelist.ScrollBars := ssVertical;
lblMinDays := TLabel.Create(Self);
lblMinDays.Parent := pnlTop;
lblMinDays.Left := memWhitelist.Left + memWhitelist.Width + 8;
lblMinDays.Top := 44;
lblMinDays.Caption := '最小天数(只预览早于此天数):';
edtMinDays := TEdit.Create(Self);
edtMinDays.Parent := pnlTop;
edtMinDays.Left := lblMinDays.Left;
edtMinDays.Top := 64;
edtMinDays.Width := 100;
edtMinDays.Text := '30';
btnPreview := TButton.Create(Self);
btnPreview.Parent := pnlTop;
btnPreview.Left := edtMinDays.Left;
btnPreview.Top := 100;
btnPreview.Width := 100;
btnPreview.Caption := '预览';
btnPreview.OnClick := BtnPreviewClick;
btnExportCSV := TButton.Create(Self);
btnExportCSV.Parent := pnlTop;
btnExportCSV.Left := btnPreview.Left + btnPreview.Width + 8;
btnExportCSV.Top := 100;
btnExportCSV.Width := 110;
btnExportCSV.Caption := '导出CSV';
btnExportCSV.OnClick := BtnExportCSVClick;
lblHint := TLabel.Create(Self);
lblHint.Parent := pnlTop;
lblHint.Left := btnExportCSV.Left + btnExportCSV.Width + 12;
lblHint.Top := 104;
lblHint.Caption := '说明:本窗口仅预览与导出,不进行删除操作。';
lvResult := TListView.Create(Self);
lvResult.Parent := Self;
lvResult.Align := alClient;
lvResult.ViewStyle := vsReport;
lvResult.ReadOnly := True;
lvResult.RowSelect := True;
lvResult.Columns.Add.Caption := '文件路径';
lvResult.Columns[0].Width := 600;
lvResult.Columns.Add.Caption := '大小';
lvResult.Columns[1].Width := 140;
lvResult.Columns.Add.Caption := '修改时间';
lvResult.Columns[2].Width := 140;
end;
procedure TfrmCleanupRulesPreview.BtnBrowseClick(Sender: TObject);
var
dlg: TFileOpenDialog;
begin
dlg := TFileOpenDialog.Create(nil);
try
dlg.Options := dlg.Options + [fdoPickFolders];
dlg.Title := '选择根目录';
if dlg.Execute then
edtRoot.Text := dlg.FileName;
finally
dlg.Free;
end;
end;
function TfrmCleanupRulesPreview.MatchAnyPattern(const FileName: string; const Patterns: TArray<string>): Boolean;
var
p, patt: string;
begin
if Length(Patterns) = 0 then Exit(True);
Result := False;
for p in Patterns do
begin
patt := Trim(p);
if patt = '' then Continue;
// 简易匹配:只支持前后通配 *
if (patt = '*') or
(patt = ExtractFileName(FileName)) or
(patt.StartsWith('*.') and SameText(ExtractFileExt(FileName), Copy(patt, 2, MaxInt))) then
begin
Result := True;
Exit;
end;
end;
end;
function TfrmCleanupRulesPreview.IsInWhitelist(const FilePath: string; const Whitelist: TArray<string>): Boolean;
var
w: string;
begin
Result := False;
for w in Whitelist do
begin
var ww := Trim(w);
if ww = '' then Continue;
if FilePath.StartsWith(IncludeTrailingPathDelimiter(ww), True) then
begin
Result := True; Exit;
end;
end;
end;
function TfrmCleanupRulesPreview.ShouldInclude(const FilePath: string; const LastWrite: TDateTime;
const Patterns, White: TArray<string>; const MinDays: Integer): Boolean;
var
days: Integer;
begin
if IsInWhitelist(FilePath, White) then
Exit(False);
if not MatchAnyPattern(FilePath, Patterns) then
Exit(False);
if (MinDays > 0) and (LastWrite > 1) then
begin
days := DaysBetween(Now, LastWrite);
if days < MinDays then
Exit(False);
end;
Result := True;
end;
procedure TfrmCleanupRulesPreview.BtnPreviewClick(Sender: TObject);
var
stack: TStack<string>;
dir: string;
files: TStringDynArray;
f: string;
size: Int64;
latest: TDateTime;
patt, white: TArray<string>;
minDays: Integer;
item: TListItem;
begin
if not TDirectory.Exists(edtRoot.Text) then
begin
MessageDlg('根目录不存在:' + edtRoot.Text, mtError, [mbOK], 0);
Exit;
end;
patt := memPatterns.Lines.ToStringArray;
white := memWhitelist.Lines.ToStringArray;
if not TryStrToInt(Trim(edtMinDays.Text), minDays) then
minDays := 0;
Screen.Cursor := crHourGlass;
try
lvResult.Items.BeginUpdate;
try
lvResult.Items.Clear;
stack := TStack<string>.Create;
try
stack.Push(ExcludeTrailingPathDelimiter(edtRoot.Text));
while stack.Count > 0 do
begin
dir := stack.Pop;
// 子目录
for var sub in TDirectory.GetDirectories(dir) do
stack.Push(sub);
// 文件
files := TDirectory.GetFiles(dir);
for f in files do
begin
try size := TFile.GetSize(f); except size := 0; end;
try latest := TFile.GetLastWriteTime(f); except latest := 0; end;
if ShouldInclude(f, latest, patt, white, minDays) then
begin
item := lvResult.Items.Add;
item.Caption := f;
item.SubItems.Add(Format('%.2f MB', [size / (1024*1024)]));
if latest > 0 then
item.SubItems.Add(DateTimeToStr(latest)) else item.SubItems.Add('-');
end;
end;
end;
finally
stack.Free;
end;
finally
lvResult.Items.EndUpdate;
end;
finally
Screen.Cursor := crDefault;
end;
end;
procedure TfrmCleanupRulesPreview.ExportListViewToCSV(LV: TListView; const FileName: string);
var
SL: TStringList; i, j: Integer; line: string;
function Esc(const S: string): string;
begin
Result := '"' + StringReplace(S, '"', '""', [rfReplaceAll]) + '"';
end;
begin
SL := TStringList.Create;
try
// header
if LV.Columns.Count > 0 then
begin
line := Esc(LV.Columns[0].Caption);
for i := 1 to LV.Columns.Count - 1 do
line := line + ',' + Esc(LV.Columns[i].Caption);
SL.Add(line);
end;
// rows
for i := 0 to LV.Items.Count - 1 do
begin
line := Esc(LV.Items[i].Caption);
for j := 0 to LV.Items[i].SubItems.Count - 1 do
line := line + ',' + Esc(LV.Items[i].SubItems[j]);
SL.Add(line);
end;
SL.SaveToFile(FileName, TEncoding.UTF8);
finally
SL.Free;
end;
end;
procedure TfrmCleanupRulesPreview.BtnExportCSVClick(Sender: TObject);
var
dlg: TSaveDialog;
begin
if lvResult.Items.Count = 0 then
begin
MessageDlg('没有数据可导出,请先预览。', mtInformation, [mbOK], 0);
Exit;
end;
dlg := TSaveDialog.Create(nil);
try
dlg.Filter := 'CSV文件 (*.csv)|*.csv|所有文件 (*.*)|*.*';
dlg.DefaultExt := 'csv';
dlg.FileName := 'CleanupPreview.csv';
if dlg.Execute then
begin
ExportListViewToCSV(lvResult, dlg.FileName);
MessageDlg('已导出到:' + dlg.FileName, mtInformation, [mbOK], 0);
end;
finally
dlg.Free;
end;
end;
end.