-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.TempFileHolder.pas
More file actions
116 lines (94 loc) · 2.79 KB
/
Copy pathMaxLogic.TempFileHolder.pas
File metadata and controls
116 lines (94 loc) · 2.79 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
unit MaxLogic.TempFileHolder;
{
deletes temporary files when it goes out of scope
}
interface
uses
system.SysUtils, system.Classes;
type
ITempFileHolder = interface
['{BFEC285A-F58B-46AC-BEB3-2827BD6E8BCD}']
function GetDirectoryList: TStringList;
function GetFileList: TStringList;
/// <summary>
/// stores the filename to the list of files that will be deleted when this class goes out of scope
/// </summary>
procedure Add(const aFileName: String);
/// <summary>
/// Adds a directory path to the list of directories that will be deleted when this instance gets out of scope
/// </summary>
/// <remarks>
/// NOTE: it will delete this directory together with all its sub items
/// </remarks>
procedure AddDirectory(const aDirectoryPath: String);
// access to the underyling lists
property FileList: TStringList read GetFileList;
property DirectoryList: TStringList read GetDirectoryList;
end;
TTempFileHolder = class(TInterfacedObject, ITempFileHolder)
private
fFiles: TStringList;
fDirectories: TStringList;
function GetDirectoryList: TStringList;
function GetFileList: TStringList;
public
constructor Create;
destructor Destroy; override;
/// <summary>
/// stores the filename to the list of files that will be deleted when this class goes out of scope
/// </summary>
procedure Add(const aFileName: String);
/// <summary>
/// Adds a directory path to the list of directories that will be deleted when this instance gets out of scope
/// </summary>
/// <remarks>
/// NOTE: it will delete this directory together with all its sub items
/// </remarks>
procedure AddDirectory(const aDirectoryPath: String);
// access to the underyling lists
property FileList: TStringList read GetFileList;
property DirectoryList: TStringList read GetDirectoryList;
end;
implementation
uses
system.IOUtils;
{ TTempFileHolder }
procedure TTempFileHolder.Add(const aFileName: String);
begin
fFiles.Add(aFileName);
end;
procedure TTempFileHolder.AddDirectory(const aDirectoryPath: String);
begin
fDirectories.Add(aDirectoryPath)
end;
constructor TTempFileHolder.Create;
begin
inherited Create;
fFiles := TStringList.Create;
fDirectories := TStringList.Create;
end;
destructor TTempFileHolder.Destroy;
begin
for var lFileName in fFiles do
begin
if TFile.Exists(lFileName) then
DeleteFile(lFileName);
end;
fFiles.Free;
for var lDir in fDirectories do
begin
if TDirectory.Exists(lDir) then
TDirectory.Delete(lDir, true);
end;
fDirectories.Free;
inherited;
end;
function TTempFileHolder.GetDirectoryList: TStringList;
begin
Result := fDirectories;
end;
function TTempFileHolder.GetFileList: TStringList;
begin
Result := fFiles;
end;
end.