forked from ARSBlue/ToolBox-4-Iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.cls
199 lines (181 loc) · 6.47 KB
/
File.cls
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
Include (arsblue.OS, arsblue.Status, arsblue.io.File, %occFile)
/// This class contains file utilities
///
/// ARSBlue ToolBox-4-Iris
/// Copyright © 2019 ARS Blue GmbH
/// http://www.ars-blue.at
Class arsblue.io.File [ Abstract ]
{
/// Get the normalized file path for given parts of file names.
/// If the first part of file names starts with a relative path (not with a file separator or drive name),
/// the current file directory will be used as location.
/// <ul>
/// <li>filename...one to many parts of file names to be concatinated and normalized to file path.</li>
/// </ul>
/// Returns the normalized file path.
ClassMethod GetFilePath(filename...) As %String
{
set:('$D(filename)) filename($I(filename))=$$$FileCurrentDir_"/"
set filepath=""
for i=1:1:filename set filepath=filepath_$S(filepath="":"",1:"/")_$TR(filename(i),"\","/")
set:(($E(filepath,1)'="/") && ($P(filepath,"/",1)'?1A1":")) filepath=$TR($$$FileCurrentDir,"\","/")_filepath
set:($E(filepath,*)=":") filepath=filepath_"/"
quit $$$FileNormalizeFilename(filepath)
}
/// Get the absolute directory path for a given file path.
/// If file path is a directory path, it returns the parent directory path.
/// <ul>
/// <li>filepath...the full file path (optional, current directory is default).</li>
/// </ul>
/// Returns the absolute directory path.
ClassMethod GetDirectory(filepath As %String = "") As %String
{
quit:(filepath="") ""
set filepath=$TR(..GetFilePath(filepath),"\","/")
set filepath=$P(filepath,"/",1,*-1)
quit $S(filepath="":"",1:$$$FileNormalizeDirectory(filepath_$S($E(filepath,*)=":":"/",1:"")))
}
/// Get the file or directory name for a given file path.
/// <ul>
/// <li>filepath...the full file path.</li>
/// </ul>
/// Returns the file or directory name.
ClassMethod GetFileName(filepath As %String = "") As %String
{
quit:(filepath="") ""
set filepath=$TR(filepath,"\","/")
set:($F("\/",$E(filepath,*))) filepath=$E(filepath,1,*-1)
quit $P($TR(filepath,"\","/"),"/",*)
}
/// Check if given file path is an existing file.
/// <ul>
/// <li>filepath...the full file path.</li>
/// </ul>
/// Returns true if file path is an existing file, false otherwise.
ClassMethod IsFile(filepath As %String = "") As %Boolean
{
quit:(filepath="") $$$NO
set filepath=..GetFilePath(filepath)
quit $S($$$FileDirectoryExists(filepath):$$$NO,1:$$$FileExists(filepath))
}
/// Check if given file path is an existing directory.
/// <ul>
/// <li>filepath...the full file path.</li>
/// </ul>
/// Returns true if file path is an existing directory, false otherwise.
ClassMethod IsDirectory(filepath As %String = "") As %Boolean
{
quit:(filepath="") $$$NO
set filepath=..GetFilePath(filepath)
quit $$$FileDirectoryExists(filepath)
}
/// Check whether given file or directory is empty.
/// <ul>
/// <li>filepath...the full file path.</li>
/// </ul>
/// Returns true if file or directory is empty, false otherwise.
ClassMethod IsEmpty(filepath As %String = "") As %Boolean
{
quit:(filepath="") $$$NO
set filepath=..GetFilePath(filepath)
if ($$$IsFile(filepath))
{
quit ($$$FileSize(filepath) <= 0)
}
elseif ($$$IsDirectory(filepath))
{
set isEmpty=$$$YES
set file=$ZSE($$$FileNormalizeDirectory(filepath)_"*")
while (file'="")
{
set filename=..GetFileName(file)
if ((filename=".") || (filename=".."))
{
set file=$ZSE("")
}
else
{
set file=$ZSE(-1)
set isEmpty=$$$NO
}
}
quit isEmpty
}
else
{
quit $$$NO
}
}
/// Delete file or directory and parent directories if they are empty.
/// <ul>
/// <li>filepath...the full file path (may contain wildcard characters).</li>
/// <li>recursive...true (default) to recursive delete empty directories, false otherwise (optional).</li>
/// </ul>
/// Returns status OK if successfully deleted file and directories (if they are empty), any other status signals failure!
ClassMethod DeleteFilePath(filepath As %String = "", recursive As %Boolean = {$$$YES}) As %Status
{
quit:(filepath="") $$$OK
set filepath=..GetFilePath(filepath)
#dim status as %Status = $$$OK
try
{
set file=$ZSE($S($F("\/",$E(filepath,*)):$E(filepath,1,*-1),1:filepath))
while (file'="")
{
set filename=..GetFileName(file)
if ((filename=".") || (filename=".."))
{
// do nothing
}
elseif ($$$IsFile(file))
{
$$$ThrowIf('##class(%File).Delete(file,.oscode),"cannot delete file ("_oscode_"): "_file)
}
elseif ($$$IsDirectory(file) && $$$IsEmpty(file))
{
$$$ThrowIf('##class(%File).RemoveDirectory(file,.oscode),"cannot delete directory ("_oscode_"): "_file)
}
set file=$ZSE("")
}
if (recursive)
{
set filepath=$$$Directory(filepath)
set:($$$IsEmpty(filepath)) status=..DeleteFilePath(filepath)
}
}
catch (exc)
{
set file=$ZSE(-1)
set status=exc.AsStatus()
}
quit status
}
/// Compare file create date with given date.
/// <ul>
/// <li>filepath...the full file path.</li>
/// <li>date...the date to compare with create date (optional, default is current timestamp).</li>
/// </ul>
/// Returns amount of seconds between file create date and given date (negative number means file is older than date, positive number means file is newer than date and zero means file has same date).
ClassMethod CompareCreateDate(filepath As %String = "", date As %TimeStamp = {$H}) As %Integer
{
quit:(filepath="") $$$MININT
quit:(date'?1.5N1","1.5N) $$$MAXINT
set filepath=..GetFilePath(filepath)
set filedate=$$$FileDateCreated(filepath) quit:(filedate'?1.5N1","1.5N) $$$MININT
quit (($P(filedate,",",1)-$P(date,",",1))*$$$DAYINSEC)+($P(filedate,",",2)-$P(date,",",2))
}
/// Compare file modification date with given date.
/// <ul>
/// <li>filepath...the full file path.</li>
/// <li>date...the date to compare with modification date (optional, default is current timestamp).</li>
/// </ul>
/// Returns amount of seconds between file modification date and given date (negative number means file is older than date, positive number means file is newer than date and zero means file has same date).
ClassMethod CompareModifiedDate(filepath As %String = "", date As %TimeStamp = {$H}) As %Integer
{
quit:(filepath="") $$$MININT
quit:(date'?1.5N1","1.5N) $$$MAXINT
set filepath=..GetFilePath(filepath)
set filedate=$$$FileDateModified(filepath) quit:(filedate'?1.5N1","1.5N) $$$MININT
quit (($P(filedate,",",1)-$P(date,",",1))*$$$DAYINSEC)+($P(filedate,",",2)-$P(date,",",2))
}
}