-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelphi_Project_Cleaner.dpr
More file actions
1505 lines (1344 loc) · 47.4 KB
/
Copy pathDelphi_Project_Cleaner.dpr
File metadata and controls
1505 lines (1344 loc) · 47.4 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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
program Delphi_Project_Cleaner;
{$APPTYPE GUI}
{$R 'Library\DP_Cleaner.res'}
uses
Windows,
SysUtils,
Classes,
StrUtils,
Math,
Registry;
const
BULLET = WideChar($2022); //Bullet Indent
APP_PATH_NAME = 'cleandpr.exe';
// -----------------------------------------------------------------
// All user-facing strings live here in one place, so wording can be
// changed or translated without hunting through the code.
// -----------------------------------------------------------------
CONSOLE_TITLE_STR = 'Delphi Project Cleaner';
HELP_HINT_STR = 'Note: Type -help for configuration options.';
SETTINGS_HEADER_STR = 'Configuration:';
HELP_INSTALL_PREFIX_STR = ' -install Registers "';
HELP_INSTALL_SUFFIX_STR = '" as a shortcut you can type into Explorer''s address bar from any folder,';
HELP_INSTALL_LINE2_STR = ' so the cleaner instantly opens and scans whichever folder you''re in.';
HELP_UNINSTALL_LINE_STR = ' -uninstall Removes the address bar shortcut from File Explorer and Startmenu.';
HELP_ABOUT_LINE_STR = ' -about Shows information about this application.';
HELP_HOME_LINE_STR = ' 0 Return to Clean Mode .';
CHOICE_PROMPT_STR = 'Choice: ';
INSTALL_SUCCESS_PREFIX_STR = 'Done! Type "';
INSTALL_SUCCESS_SUFFIX_STR = '" in Explorer''s address bar any time to clean the current folder.';
INSTALL_FAIL_STR = 'Failed to install the address-bar shortcut.';
UNINSTALL_SUCCESS_STR = 'Address-bar shortcut removed successfully.';
UNINSTALL_NOOP_STR = 'Nothing to remove - the shortcut wasn''t installed.';
UNRECOGNIZED_OPTION_STR = 'Invalid choice: ';
DEFAULT_PAGE_STR = 'You are now in Clean Mode.';
ENTER_PATH_PROMPT_STR = 'Please Delphi enter project path(s), or press Esc to exit.';
PATH_PROMPT_STR = 'Path: ';
NO_PROJECT_CURRENT_DIR_STR = 'No valid Delphi project found in the current directory.';
PATH_EMPTY_STR = 'Path cannot be empty.';
INVALID_PATH_STR = 'Invalid path. The directory does not exist.';
NO_PROJECT_SPECIFIED_PATH_STR = 'No valid Delphi project found in the specified path: ';
CURRENT_PATH_STR = 'Current Path: ';
PROJECTS_LIST_HEADER_STR = 'Delphi Projects List:';
SCAN_PROJECTS_STR = 'Scanning for Delphi projects';
SCAN_PROJECT_FOLDERS_STR = 'Scanning project folders';
SCAN_LEFTOVER_FILES_STR = 'Scanning for leftover files';
SCAN_ORPHANED_FILES_STR = 'Scanning for orphaned files';
SCAN_HISTORY_FOLDERS_STR = 'Scanning for build history folders';
SCAN_COMPLETE_STR = 'Scanning complete';
DELETING_FILES_STR = 'Deleting files';
REMOVING_EMPTY_FOLDERS_STR = 'Removing empty folders';
ERROR_DELETING_PREFIX_STR = ' Error deleting: ';
ERROR_DELETING_SEP_STR = ' - ';
ALREADY_CLEANED_STR = 'All selected projects are already clean.';
DELETED_LIST_HEADER_STR = 'Deleted File List:';
OTHER_FILES_STR = ' Other Files:';
PRESS_ANY_KEY_STR = 'Press any key to exit.';
ERROR_PREFIX_STR = 'Error: ';
PRESS_ESC_STR = 'Press ESC to exit.';
// -----------------------------------------------------------------
// "-about" screen contents.
// -----------------------------------------------------------------
ABOUT_TITLE_STR = 'Delphi Projects Cleaner v2.0';
ABOUT_DEVELOPER_STR = 'Developer: Shohanur Rahman';
ABOUT_PUBLISHER_STR = 'Publisher: SR Studio 24 - BD';
ABOUT_RELEASE_DATE_STR = 'Release Date: August 03, 2026';
ABOUT_VISIT_STR = 'To explore more amazing tools from SR Studio 24 - BD, visit:';
ABOUT_WEBSITE_STR = 'Publisher Website: https://srstudio24.blogspot.com/';
var BasePath: string; DPRFiles: TStringList;
procedure ShowConsole;
begin
if not AllocConsole then Exit;
SetConsoleOutputCP(CP_UTF8);
SetConsoleTitle(CONSOLE_TITLE_STR);
end;
function JoinPath(const Path1, Path2: string): string;
begin
Result := IncludeTrailingPathDelimiter(Path1) + Path2;
end;
procedure WriteConsoleLine(const S: string);
var
hStdOut: THandle;
Written: DWORD;
Line: string;
ConsoleMode: DWORD;
begin
hStdOut := GetStdHandle(STD_OUTPUT_HANDLE);
if GetConsoleMode(hStdOut, ConsoleMode) then
begin
Line := S + sLineBreak;
WriteConsoleW(hStdOut, PWideChar(Line), Length(Line), Written, nil);
end
else
Writeln(S);
end;
procedure WriteConsoleText(const S: string);
var
hStdOut: THandle;
Written: DWORD;
ConsoleMode: DWORD;
begin
hStdOut := GetStdHandle(STD_OUTPUT_HANDLE);
if GetConsoleMode(hStdOut, ConsoleMode) then
WriteConsoleW(hStdOut, PWideChar(S), Length(S), Written, nil)
else
Write(S);
end;
procedure WriteBlankLine;
begin
WriteConsoleLine('');
end;
// ---------------------------------------------------------------------
// Cursor position helpers, used to print text below the "Path: " input
// line while keeping the actual typing cursor sitting right after
// "Path: " (see PrintPathPrompt), and to keep the hint block pushed
// out of the way of long/pasted input (see EnsureHintPosition below).
// ---------------------------------------------------------------------
function GetCursorPos: TCoord;
var
Info: TConsoleScreenBufferInfo;
begin
if GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), Info) then
Result := Info.dwCursorPosition
else
begin
Result.X := 0;
Result.Y := 0;
end;
end;
procedure SetCursorPos(const Pos: TCoord);
begin
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
end;
function GetConsoleBufferWidth: SmallInt;
var
Info: TConsoleScreenBufferInfo;
begin
if GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), Info) then
Result := Info.dwSize.X
else
Result := 80;
end;
// Clears one full console row (used to erase the hint block from its
// old position before it gets redrawn further down).
procedure ClearRow(Row: SmallInt);
var
Pos: TCoord;
begin
Pos.X := 0;
Pos.Y := Row;
SetCursorPos(Pos);
WriteConsoleText(StringOfChar(' ', GetConsoleBufferWidth));
end;
procedure WriteAtRow(Row: SmallInt; const Text: string);
var
Pos: TCoord;
begin
Pos.X := 0;
Pos.Y := Row;
SetCursorPos(Pos);
WriteConsoleText(Text);
end;
var HelpHintShown: Boolean = False; HelpHintPending: Boolean = False; HelpHintTopRow: SmallInt = 0;
procedure EnsureHintPosition;
var
CurPos: TCoord;
NewTopRow: SmallInt;
begin
if not HelpHintPending then
Exit;
CurPos := GetCursorPos;
// Hint block still safely below the typing row - nothing to do.
if CurPos.Y < HelpHintTopRow then
Exit;
NewTopRow := CurPos.Y + 1;
// Erase the old 3-line block (blank, Note, dashes).
ClearRow(HelpHintTopRow);
ClearRow(HelpHintTopRow + 1);
ClearRow(HelpHintTopRow + 2);
// Redraw it one row below the current typing row.
WriteAtRow(NewTopRow, '');
WriteAtRow(NewTopRow + 1, HELP_HINT_STR);
WriteAtRow(NewTopRow + 2, StringOfChar('-', Length(HELP_HINT_STR)));
HelpHintTopRow := NewTopRow;
// Restore the real typing cursor - all of the above moved it around.
SetCursorPos(CurPos);
end;
var LastProgressLen: Integer = 0;
procedure ProgressUpdate(const Caption: string; Percent: Integer);
var
Line: string;
begin
if Percent < 0 then Percent := 0;
if Percent > 100 then Percent := 100;
Line := Caption + ' ... ' + IntToStr(Percent) + '%';
WriteConsoleText(#13 + Line + StringOfChar(' ', Max(0, LastProgressLen - Length(Line))));
LastProgressLen := Length(Line);
end;
procedure ProgressClear;
begin
if LastProgressLen > 0 then
begin
WriteConsoleText(#13 + StringOfChar(' ', LastProgressLen) + #13);
LastProgressLen := 0;
end;
end;
procedure PressAnyKeyToExit;
var
hStdIn: THandle;
InputRec: TInputRecord;
NumRead: DWORD;
VK: Word;
IsModifierKey: Boolean;
begin
hStdIn := GetStdHandle(STD_INPUT_HANDLE);
repeat
if not ReadConsoleInput(hStdIn, InputRec, 1, NumRead) then
Break;
if (InputRec.EventType = KEY_EVENT) and InputRec.Event.KeyEvent.bKeyDown then
begin
VK := InputRec.Event.KeyEvent.wVirtualKeyCode;
IsModifierKey :=
(VK = VK_SHIFT) or (VK = VK_LSHIFT) or (VK = VK_RSHIFT) or
(VK = VK_CONTROL) or (VK = VK_LCONTROL) or (VK = VK_RCONTROL) or
(VK = VK_MENU) or (VK = VK_LMENU) or (VK = VK_RMENU) or
(VK = VK_LWIN) or (VK = VK_RWIN) or
(VK = VK_CAPITAL) or (VK = VK_NUMLOCK) or (VK = VK_SCROLL);
if not IsModifierKey then
Break;
end;
until False;
end;
procedure PressEscToExit;
var
hStdIn: THandle;
InputRec: TInputRecord;
NumRead: DWORD;
VK: Word;
begin
hStdIn := GetStdHandle(STD_INPUT_HANDLE);
repeat
if not ReadConsoleInput(hStdIn, InputRec, 1, NumRead) then
Break;
if (InputRec.EventType = KEY_EVENT) and InputRec.Event.KeyEvent.bKeyDown then
begin
VK := InputRec.Event.KeyEvent.wVirtualKeyCode;
// Check for ESC key
if VK = VK_ESCAPE then
Halt(0);
end;
until False;
end;
type TAfterCharProc = procedure;
function ReadLineEsc(out LineOut: string; AfterCharProc: TAfterCharProc = nil): Boolean;
var hStdIn, hStdOut: THandle; InputRec: TInputRecord; NumRead: DWORD; VK: Word;
Ch: WideChar; Buffer, TempLine: string; CursorPos: Integer; StartX, StartY: SmallInt;
ConsoleInfo: TConsoleScreenBufferInfo; OriginalCursor: TCoord;
begin
Buffer := '';
CursorPos := 0;
hStdIn := GetStdHandle(STD_INPUT_HANDLE);
hStdOut := GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdOut, ConsoleInfo);
StartX := ConsoleInfo.dwCursorPosition.X;
StartY := ConsoleInfo.dwCursorPosition.Y;
repeat
if not ReadConsoleInput(hStdIn, InputRec, 1, NumRead) then
Break;
if (InputRec.EventType = KEY_EVENT) and InputRec.Event.KeyEvent.bKeyDown then
begin
VK := InputRec.Event.KeyEvent.wVirtualKeyCode;
Ch := InputRec.Event.KeyEvent.UnicodeChar;
if VK = VK_ESCAPE then
begin
LineOut := '';
Result := False;
Exit;
end
else if (VK = VK_RETURN) then
begin
WriteConsoleLine('');
LineOut := Buffer;
Result := True;
Exit;
end
else if (VK = VK_LEFT) and (CursorPos > 0) then
begin
Dec(CursorPos);
// Move cursor left
OriginalCursor.X := StartX + CursorPos;
OriginalCursor.Y := StartY;
SetConsoleCursorPosition(hStdOut, OriginalCursor);
if Assigned(AfterCharProc) then
AfterCharProc;
end
else if (VK = VK_RIGHT) and (CursorPos < Length(Buffer)) then
begin
Inc(CursorPos);
// Move cursor right
OriginalCursor.X := StartX + CursorPos;
OriginalCursor.Y := StartY;
SetConsoleCursorPosition(hStdOut, OriginalCursor);
if Assigned(AfterCharProc) then
AfterCharProc;
end
else if (VK = VK_DELETE) and (CursorPos < Length(Buffer)) then
begin
// Delete character at cursor position
Delete(Buffer, CursorPos + 1, 1);
// Redraw from cursor position
TempLine := Copy(Buffer, CursorPos + 1, MaxInt);
// Move cursor to current position
OriginalCursor.X := StartX + CursorPos;
OriginalCursor.Y := StartY;
SetConsoleCursorPosition(hStdOut, OriginalCursor);
// Write the rest of the line
WriteConsoleText(TempLine + ' ');
// Move cursor back to correct position
OriginalCursor.X := StartX + CursorPos;
OriginalCursor.Y := StartY;
SetConsoleCursorPosition(hStdOut, OriginalCursor);
if Assigned(AfterCharProc) then
AfterCharProc;
end
else if (VK = VK_BACK) then
begin
if CursorPos > 0 then
begin
// Delete character before cursor
Delete(Buffer, CursorPos, 1);
Dec(CursorPos);
// Redraw from cursor position
TempLine := Copy(Buffer, CursorPos + 1, MaxInt);
OriginalCursor.X := StartX + CursorPos;
OriginalCursor.Y := StartY;
SetConsoleCursorPosition(hStdOut, OriginalCursor);
WriteConsoleText(TempLine + ' ');
// Move cursor back
OriginalCursor.X := StartX + CursorPos;
OriginalCursor.Y := StartY;
SetConsoleCursorPosition(hStdOut, OriginalCursor);
if Assigned(AfterCharProc) then
AfterCharProc;
end;
end
else if (Ch <> #0) and not (Ch < #32) then
begin
// Insert character at cursor position
Insert(Ch, Buffer, CursorPos + 1);
Inc(CursorPos);
// Redraw from cursor position
TempLine := Copy(Buffer, CursorPos, MaxInt);
OriginalCursor.X := StartX + CursorPos - 1;
OriginalCursor.Y := StartY;
SetConsoleCursorPosition(hStdOut, OriginalCursor);
WriteConsoleText(TempLine);
// Move cursor to after inserted character
OriginalCursor.X := StartX + CursorPos;
OriginalCursor.Y := StartY;
SetConsoleCursorPosition(hStdOut, OriginalCursor);
if Assigned(AfterCharProc) then
AfterCharProc;
end;
end;
until False;
LineOut := Buffer;
Result := True;
end;
function HasExtension(const FileName, Extension: string): Boolean;
begin
Result := EndsText(Extension, FileName);
end;
function IsDprojVariantFile(const FileName: string): Boolean;
var
P: Integer;
begin
P := Pos('.dproj.', LowerCase(FileName));
Result := (P > 1) and (P + Length('.dproj.') <= Length(FileName));
end;
function IsGrouprocVariantFile(const FileName: string): Boolean;
var
P: Integer;
begin
// Check for .groupproj. (with 'j' at the end)
P := Pos('.groupproj.', LowerCase(FileName));
Result := (P > 1) and (P + Length('.groupproj.') <= Length(FileName));
end;
function RelativeToBase(const FullPath: string): string;
begin
Result := FullPath;
if AnsiStartsText(BasePath, Result) then
begin
Result := Copy(Result, Length(BasePath) + 1, MaxInt);
if (Length(Result) > 0) and (Result[1] = '\') then
Result := Copy(Result, 2, MaxInt);
end;
end;
procedure FindFilesRecursive(const Path, Extension: string; FileList: TStringList);
var
SearchRec: TSearchRec;
FullPath: string;
begin
if FindFirst(JoinPath(Path, '*'), faAnyFile, SearchRec) = 0 then
try
repeat
if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
Continue;
FullPath := JoinPath(Path, SearchRec.Name);
if (SearchRec.Attr and faDirectory) = faDirectory then
begin
FindFilesRecursive(FullPath, Extension, FileList);
end
else if HasExtension(SearchRec.Name, Extension) then
begin
FileList.Add(FullPath);
end;
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
end;
procedure FindDprojVariantFiles(const Path, BaseName: string; FileList: TStringList);
var
SearchRec: TSearchRec;
FullPath: string;
begin
if FindFirst(JoinPath(Path, '*'), faAnyFile, SearchRec) = 0 then
try
repeat
if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
Continue;
FullPath := JoinPath(Path, SearchRec.Name);
if (SearchRec.Attr and faDirectory) = faDirectory then
FindDprojVariantFiles(FullPath, BaseName, FileList)
else if StartsText(BaseName + '.dproj.', SearchRec.Name) then
FileList.Add(FullPath);
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
end;
// Renamed from FindOrphanDprojVariantFiles: now catches both ".dproj.*"
// AND ".grouppro.*" leftover variant files (e.g. .grouppro.local,
// .grouppro.tmp, .grouppro.any...) in a single directory walk.
procedure FindOrphanVariantFiles(const Path: string; FileList: TStringList);
var
SearchRec: TSearchRec;
FullPath: string;
begin
if FindFirst(JoinPath(Path, '*'), faAnyFile, SearchRec) = 0 then
try
repeat
if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
Continue;
FullPath := JoinPath(Path, SearchRec.Name);
if (SearchRec.Attr and faDirectory) = faDirectory then
FindOrphanVariantFiles(FullPath, FileList)
else if IsDprojVariantFile(SearchRec.Name) or IsGrouprocVariantFile(SearchRec.Name) then
FileList.Add(FullPath);
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
end;
procedure FindFoldersRecursive(const Path, FolderName: string; FolderList: TStringList;
MatchPrefix: Boolean = False);
var
SearchRec: TSearchRec;
FullPath: string;
IsMatch: Boolean;
begin
if FindFirst(JoinPath(Path, '*'), faAnyFile, SearchRec) = 0 then
try
repeat
if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
Continue;
if (SearchRec.Attr and faDirectory) = faDirectory then
begin
FullPath := JoinPath(Path, SearchRec.Name);
if MatchPrefix then
IsMatch := StartsText(FolderName, SearchRec.Name)
else
IsMatch := SameText(SearchRec.Name, FolderName);
if IsMatch then
FolderList.Add(FullPath);
FindFoldersRecursive(FullPath, FolderName, FolderList, MatchPrefix);
end;
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
end;
procedure DeleteFolderRecursive(const Path: string);
var
SearchRec: TSearchRec;
FullPath: string;
begin
if FindFirst(JoinPath(Path, '*'), faAnyFile, SearchRec) = 0 then
try
repeat
if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
Continue;
FullPath := JoinPath(Path, SearchRec.Name);
if (SearchRec.Attr and faDirectory) = faDirectory then
DeleteFolderRecursive(FullPath)
else
begin
SetFileAttributes(PChar(FullPath), FILE_ATTRIBUTE_ARCHIVE);
DeleteFile(FullPath);
end;
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
RemoveDir(Path);
end;
procedure DeleteEmptyFolders(const Path: string);
var
SearchRec: TSearchRec;
FullPath: string;
SubDirs: TStringList;
DirName: string;
IsEmpty: Boolean;
begin
SubDirs := TStringList.Create;
try
if FindFirst(JoinPath(Path, '*'), faAnyFile, SearchRec) = 0 then
try
repeat
if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
Continue;
if (SearchRec.Attr and faDirectory) = faDirectory then
begin
FullPath := JoinPath(Path, SearchRec.Name);
SubDirs.Add(FullPath);
end;
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
for DirName in SubDirs do DeleteEmptyFolders(DirName);
IsEmpty := True;
if FindFirst(JoinPath(Path, '*'), faAnyFile, SearchRec) = 0 then
try
repeat
if (SearchRec.Name = '.') or (SearchRec.Name = '..') then Continue;
IsEmpty := False;
Break;
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
if IsEmpty then
begin
try
RemoveDir(Path);
except
end;
end;
finally
SubDirs.Free;
end;
end;
function CountNameOccurrences(List: TStringList; const Name: string): Integer;
var idx: Integer;
begin
Result := 0;
for idx := 0 to List.Count - 1 do
if SameText(List[idx], Name) then
Inc(Result);
end;
function StripSurroundingQuotes(const S: string): string;
var StartPos, EndPos: Integer;
begin
Result := S;
StartPos := 1;
while (StartPos <= Length(Result)) and (Result[StartPos] = '"') do
Inc(StartPos);
EndPos := Length(Result);
while (EndPos >= 1) and (Result[EndPos] = '"') do
Dec(EndPos);
if StartPos <= EndPos then
Result := Copy(Result, StartPos, EndPos - StartPos + 1)
else Result := '';
Result := Trim(Result);
end;
// ---------------------------------------------------------------------
// "Type a short name in Explorer's address bar" support, the same
// mechanism that makes typing "cmd" in the address bar open a command
// prompt in the current folder. Explorer looks the name up under
// App Paths and launches it with the current folder as the working
// directory - no command-line argument is passed, so the program falls
// back to GetCurrentDir() (see FirstRun logic below) to pick it up.
//
// This is the ONLY installation mechanism the app supports - there is
// no right-click / Send-To context menu integration.
// ---------------------------------------------------------------------
function InstallAppPathsEntry: Boolean;
var
Reg: TRegistry;
ExePath, ExeFolder: string;
begin
Result := False;
ExePath := ParamStr(0);
ExeFolder := ExtractFilePath(ExePath);
Reg := TRegistry.Create(KEY_WRITE);
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\App Paths\' + APP_PATH_NAME, True) then
begin
Reg.WriteString('', ExePath); // (Default) = full path to the exe
Reg.WriteString('Path', ExeFolder); // helps locate any DLLs next to the exe
Reg.CloseKey;
Result := True;
end;
finally
Reg.Free;
end;
end;
function UninstallAppPathsEntry: Boolean;
var
Reg: TRegistry;
begin
Reg := TRegistry.Create(KEY_WRITE);
try
Reg.RootKey := HKEY_CURRENT_USER;
Result := Reg.DeleteKey('Software\Microsoft\Windows\CurrentVersion\App Paths\' + APP_PATH_NAME);
finally
Reg.Free;
end;
end;
function IsAppPathsInstalled: Boolean;
var
Reg: TRegistry;
begin
Reg := TRegistry.Create(KEY_READ);
try
Reg.RootKey := HKEY_CURRENT_USER;
Result := Reg.KeyExists('Software\Microsoft\Windows\CurrentVersion\App Paths\' + APP_PATH_NAME);
finally
Reg.Free;
end;
end;
// ---------------------------------------------------------------------
// "-about" screen: shows application/publisher information. Callable
// both from the "-help" configuration menu and directly from the
// default Clean Mode prompt.
// ---------------------------------------------------------------------
procedure ShowAboutBlock;
begin
WriteConsoleLine(ABOUT_TITLE_STR);
WriteConsoleLine(StringOfChar('-', Length(ABOUT_TITLE_STR)));
WriteConsoleLine(ABOUT_DEVELOPER_STR);
WriteConsoleLine(ABOUT_PUBLISHER_STR);
WriteConsoleLine(ABOUT_RELEASE_DATE_STR);
WriteBlankLine;
WriteConsoleLine(ABOUT_VISIT_STR);
WriteConsoleLine(ABOUT_WEBSITE_STR);
WriteConsoleLine(StringOfChar('-', Length(ABOUT_WEBSITE_STR)));
WriteBlankLine;
end;
// ---------------------------------------------------------------------
// "-help" screen: lets the user install/uninstall the Explorer
// address-bar shortcut ("dpclean"), or view "-about" info, from inside
// the running app, without needing to pass a command-line switch.
// Returns True if the user chose "0" (go back to the normal prompt),
// or False if the user pressed ESC (caller should exit the app).
// ---------------------------------------------------------------------
function ShowHelpMenu: Boolean;
var
Choice: string;
GotLine: Boolean;
ResultMsg: string;
begin
// NOTE: no initial "Result := True" here - both exit paths below
// (ESC -> False, "0" -> True) always set Result immediately before
// Exit, so a default assignment here was dead code (DCC H2077).
// Header/menu is printed once on entry, not repeated after every choice.
WriteBlankLine;
WriteConsoleLine(SETTINGS_HEADER_STR);
WriteConsoleLine(StringOfChar('-', Length(SETTINGS_HEADER_STR)));
WriteConsoleLine(HELP_INSTALL_PREFIX_STR + ChangeFileExt(APP_PATH_NAME, '') + HELP_INSTALL_SUFFIX_STR);
WriteConsoleLine(HELP_INSTALL_LINE2_STR);
WriteConsoleLine(HELP_UNINSTALL_LINE_STR);
WriteConsoleLine(HELP_ABOUT_LINE_STR);
WriteBlankLine;
WriteConsoleLine(HELP_HOME_LINE_STR);
WriteBlankLine;
while True do
begin
WriteConsoleText(CHOICE_PROMPT_STR);
GotLine := ReadLineEsc(Choice);
if not GotLine then
begin
Result := False;
Exit;
end;
Choice := Trim(Choice);
if Choice = '0' then
begin
Result := True;
Exit;
end
else if SameText(Choice, '-install') then
begin
if InstallAppPathsEntry then
ResultMsg := INSTALL_SUCCESS_PREFIX_STR + ChangeFileExt(APP_PATH_NAME, '') + INSTALL_SUCCESS_SUFFIX_STR
else
ResultMsg := INSTALL_FAIL_STR;
end
else if SameText(Choice, '-uninstall') then
begin
if UninstallAppPathsEntry then
ResultMsg := UNINSTALL_SUCCESS_STR
else
ResultMsg := UNINSTALL_NOOP_STR;
end
else if SameText(Choice, '-about') then
begin
ShowAboutBlock;
ResultMsg := ''; // ShowAboutBlock already prints its own separators/blank line
end
else if Choice <> '' then
ResultMsg := UNRECOGNIZED_OPTION_STR + Choice
else
ResultMsg := '';
// After each processed choice, print only the result and a separator
// line matching its width - not the whole menu again.
if ResultMsg <> '' then
begin
WriteConsoleLine(ResultMsg);
WriteConsoleLine(StringOfChar('-', Length(ResultMsg)));
WriteBlankLine;
end;
end;
end;
// ---------------------------------------------------------------------
// The "Note: Type -help for settings or instructions." hint only needs
// to be shown once per run. The very first time PrintPathPrompt runs,
// it also prints "Path: " itself, remembers exactly where the cursor
// sits at that point (i.e. where the user is meant to type), prints the
// hint plus a dashed line underneath starting one row below that, then
// moves the cursor back up to the remembered spot - so the note is
// visible on screen but typing still happens right after "Path: ", not
// below the note.
//
// While the user is typing/pasting, EnsureHintPosition (declared above,
// wired in via ReadLineEsc's AfterCharProc) keeps pushing the hint block
// further down the moment console line-wrap would otherwise carry the
// typed text into it - so a long pasted path can never overwrite it.
//
// Returns True if it already printed "Path: " itself, so the caller's
// loop must skip printing it again for that one iteration.
//
// Once the user finishes typing and presses Enter, AdvancePastHintIfPending
// must be called: it moves the cursor down past the blank/note/dash lines
// (wherever they currently are, since EnsureHintPosition may have moved
// them) and adds one blank line, so everything printed afterwards (error
// messages, the next prompt, etc.) continues normally below the hint
// instead of overlapping and garbling it.
// ---------------------------------------------------------------------
function PrintPathPrompt: Boolean;
var
InputPos: TCoord;
begin
Result := False;
WriteConsoleLine(ENTER_PATH_PROMPT_STR);
if not HelpHintShown then
begin
WriteConsoleText(PATH_PROMPT_STR);
InputPos := GetCursorPos;
HelpHintTopRow := InputPos.Y + 1;
WriteAtRow(HelpHintTopRow, '');
WriteAtRow(HelpHintTopRow + 1, HELP_HINT_STR);
WriteAtRow(HelpHintTopRow + 2, StringOfChar('-', Length(HELP_HINT_STR)));
SetCursorPos(InputPos);
HelpHintShown := True;
HelpHintPending := True;
Result := True;
end;
end;
procedure AdvancePastHintIfPending;
var
Pos: TCoord;
begin
if not HelpHintPending then
Exit;
HelpHintPending := False;
// HelpHintTopRow may have been pushed down by EnsureHintPosition while
// the user was typing, so always read it fresh rather than assuming a
// fixed offset from the original "Path: " row.
Pos.X := 0;
Pos.Y := HelpHintTopRow + 3;
SetCursorPos(Pos);
WriteBlankLine;
end;
// This function asks for project path - shows full message only when no project found
// Also accepts "-help" to open the settings/instructions screen, and
// "-install" / "-uninstall" / "-about" to run those actions directly
// from Clean Mode without going through the "-help" submenu.
// Returns '' and Halt(0)s the app if the user presses ESC anywhere.
function AskForProjectPath(const ShowNoProjectMessage: Boolean = True): string;
var
InputPath: string;
GotLine: Boolean;
ErrMsg: string;
ResultMsg: string;
DefaultMsg: string;
SkipPathPrompt: Boolean;
begin
Result := '';
if ShowNoProjectMessage then
begin
WriteBlankLine;
WriteConsoleLine(NO_PROJECT_CURRENT_DIR_STR);
end;
SkipPathPrompt := PrintPathPrompt;
repeat
if SkipPathPrompt then
SkipPathPrompt := False
else
WriteConsoleText(PATH_PROMPT_STR);
GotLine := ReadLineEsc(InputPath, @EnsureHintPosition);
if not GotLine then
begin
// User pressed ESC - exit immediately
Halt(0);
end;
// If the hint block was drawn below this "Path: " line, move the
// cursor past it now (before printing anything else) so the error
// message / next prompt / etc. appear cleanly below it instead of
// overlapping the Note/dash lines.
AdvancePastHintIfPending;
InputPath := Trim(InputPath);
if SameText(InputPath, '-help') then
begin
if not ShowHelpMenu then
Halt(0); // ESC pressed inside help menu
DefaultMsg := DEFAULT_PAGE_STR;
WriteConsoleLine(DefaultMsg);
WriteConsoleLine(StringOfChar('-', Length(DefaultMsg)));
WriteBlankLine;
SkipPathPrompt := PrintPathPrompt;
Continue;
end
else if SameText(InputPath, '-install') then
begin
if InstallAppPathsEntry then
ResultMsg := INSTALL_SUCCESS_PREFIX_STR + ChangeFileExt(APP_PATH_NAME, '') + INSTALL_SUCCESS_SUFFIX_STR
else
ResultMsg := INSTALL_FAIL_STR;
WriteConsoleLine(ResultMsg);
WriteConsoleLine(StringOfChar('-', Length(ResultMsg)));
WriteBlankLine;
SkipPathPrompt := PrintPathPrompt;
Continue;
end
else if SameText(InputPath, '-uninstall') then
begin
if UninstallAppPathsEntry then
ResultMsg := UNINSTALL_SUCCESS_STR
else
ResultMsg := UNINSTALL_NOOP_STR;
WriteConsoleLine(ResultMsg);
WriteConsoleLine(StringOfChar('-', Length(ResultMsg)));
WriteBlankLine;
SkipPathPrompt := PrintPathPrompt;
Continue;
end
else if SameText(InputPath, '-about') then
begin
ShowAboutBlock;
SkipPathPrompt := PrintPathPrompt;
Continue;
end;
InputPath := StripSurroundingQuotes(InputPath);
InputPath := Trim(InputPath);
if InputPath = '' then
ErrMsg := PATH_EMPTY_STR
else if not DirectoryExists(InputPath) then
ErrMsg := INVALID_PATH_STR
else
ErrMsg := '';
if ErrMsg <> '' then
begin
// Show just the error and a separator matching its width instead
// of repeating the whole "Please enter the project path..." block.
WriteConsoleLine(ErrMsg);
WriteConsoleLine(StringOfChar('-', Length(ErrMsg)));
WriteBlankLine;