-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_FileUtil.ahk
313 lines (249 loc) · 6.07 KB
/
class_FileUtil.ahk
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
class FileUtil {
static void := FileUtil._init()
_init() {
}
__New() {
throw Exception( "FileUtil is a static class, dont instante it!", -1 )
}
getDir( path ) {
path := RegExReplace( path, "^(.*?)\\$", "$1" )
if( this.isDir(path) )
return path
return this.getParentDir( path )
}
getParentDir( path ) {
path := RegExReplace( path, "^(.*?)\\$", "$1" )
path := RegExReplace( path, "^(.*)\\.+?$", "$1" )
return path
}
getExt( filePath ) {
SplitPath, % filePath, fileName, fileDir, fileExtention, fileNameWithoutExtension, DriveName
StringLower, fileExtention, fileExtention
return fileExtention
}
/**
* File extention is matched width extentionPattern
*
* @param {string} filePath
* @param {string} extentionPattern
* @exmaple
* FileUtil.isExt("cue|mdx")
*/
isExt( filePath, extentionPattern ) {
IfNotExist %filePath%
return false
if ( RegExMatch( filePath, "i).*\.(" extentionPattern ")$" ) ) {
return true
} else {
return false
}
}
getFileName( filePath, withExt:=true ) {
filePath := RegExReplace( filePath, "^(.*?)\\$", "$1" )
SplitPath, filePath, fileName, fileDir, fileExtention, fileNameWithoutExtension, DriveName
if( withExt == true )
return fileName
return fileNameWithoutExtension
}
getFiles( path, pattern=".*", includeDir=false, recursive=false ) {
files := []
if ( this.isFile(path) ) {
if RegExMatch( path, pattern )
files.Insert( path )
} else {
currDir := this.getDir( path )
Loop, %currDir%\*, % includeDir, % recursive
{
if not RegExMatch( A_LoopFileFullPath, pattern )
continue
files.Insert( A_LoopFileFullPath )
}
this._sortArray( files )
}
return files
}
getFile( pathDirOrFile, pattern=".*" ) {
if ( pathDirOrFile == "" or this.isFile(pathDirOrFile) ) {
return pathDirOrFile
}
files := this.getFiles( pathDirOrFile, pattern )
if ( files.MaxIndex() > 0 ) {
return files[ 1 ]
}
return ""
}
isDir( path ) {
if( ! this.exist(path) )
return false
FileGetAttrib, attr, %path%
Return InStr( attr, "D" )
}
isFile( path ) {
if( ! this.exist(path) )
return false
FileGetAttrib, attr, %path%
Return ! InStr( attr, "D" )
}
readProperties( path ) {
prop := []
Loop, Read, %path%
{
If RegExMatch(A_LoopReadLine, "^#.*" )
continue
splitPosition := InStr(A_LoopReadLine, "=" )
If ( splitPosition = 0 ) {
key := A_LoopReadLine
val := ""
} else {
key := SubStr( A_LoopReadLine, 1, splitPosition - 1 )
val := SubStr( A_LoopReadLine, splitPosition + 1 )
}
prop[ Trim(key) ] := Trim(val)
}
return prop
}
makeDir( path ) {
FileCreateDir, %path%
}
makeParentDir( path, forDirectory=true ) {
if ( forDirectory == true ) {
parentDir := this.getParentDir( path )
} else {
parentDir := this.getDir( path )
}
FileCreateDir, % parentDir
}
exist( path ) {
return FileExist( path )
}
delete( path, recursive=1 ) {
if ( this.isFile(path) ) {
FileDelete, % path
} else if( this.isDir(path) ) {
FileRemoveDir, % path, % recursive
}
}
move( src, trg, overwrite=1 ) {
if ( ! this.exist(src) )
return
this.makeParentDir( trg, this.isDir(src) )
FileMove, % src, % trg, % overwrite
}
copy( src, trg, overwrite=1 ) {
if ( ! this.exist(src) )
return
this.makeParentDir( trg, this.isDir(src) )
if ( this.isDir(src) ) {
FileCopyDir, % src, % trg, % overwrite
} else {
FileCopy, % src, % trg, % overwrite
}
}
/**
* get file size
*
* @param {path} filePath
* @return size (byte)
*/
getSize( path ) {
FileGetSize, size, % path
return size
}
/**
* get time
*
* @param {path} file path
* @param {witchTime} M: modification time (default), C: creation time, A: last access time
* @return YYYYMMDDHH24MISS
*/
getTime( path, whichTime="M" ) {
FileGetTime, var, % path, % whichTime
return var
}
/**
* Get Symbolic Link Information
*
* @param filePath path to check if it is symbolic link
* @param srcPath path to linked by filePath
* @param linkType link type ( file or directory )
* @return true if filepath is symbolic link
*/
isSymlink( filePath, ByRef srcPath="", ByRef linkType="" ) {
IfNotExist, % filePath
return false
if RegExMatch(filePath,"^\w:\\?$") ; false if it is a root directory
return false
SplitPath, filePath, fn, parentDir
result := this.cli( "/c dir /al """ (InStr(FileExist(filePath),"D") ? parentDir "\" : filePath) """" )
if RegExMatch(result,"<(.+?)>.*?\b" fn "\b.*?\[(.+?)\]",m) {
linkType:= m1, srcPath := m2
if ( linkType == "SYMLINK" )
linkType := "file"
else if ( linkType == "SYMLINKD" )
linkType := "directory"
return true
} else {
return false
}
}
/**
* make symbolic link
*
* @param src source path (real file)
* @param trg target path (path to used as link)
*/
makeLink( src, trg ) {
if this.isSymlink( trg ) {
this.delete( trg )
}
this.makeParentDir( trg, this.isDir(src) )
if ( this.isDir(src) ) {
cmd := "/c mklink /d """ trg """ """ src """"
} else {
cmd := "/c mklink /f """ trg """ """ src """"
}
this.cli( cmd )
}
/**
* run command and return result
*
* @param command command
* @return command execution result
*/
cli( command ) {
dhw := A_DetectHiddenWindows
DetectHiddenWindows,On
Run, %ComSpec% /k,,Hide UseErrorLevel, pid
if not ErrorLevel
{
while ! WinExist("ahk_pid" pid)
Sleep,100
DllCall( "AttachConsole","UInt",pid )
}
DetectHiddenWindows, % dhw
; debug( "command :`n`t" command )
shell := ComObjCreate("WScript.Shell")
try {
exec := shell.Exec( comspec " " command )
While ! exec.Status
sleep, 100
result := exec.StdOut.readAll()
}
catch e
{
debug( "error`n" e.what "`n" e.message )
}
; debug( "result :`n`t" result )
DllCall("FreeConsole")
Process Close, %pid%
return result
}
_sortArray( Array ) {
t := Object()
for k, v in Array
t[RegExReplace(v,"\s")]:=v
for k, v in t
Array[A_Index] := v
return Array
}
}