-
Notifications
You must be signed in to change notification settings - Fork 37
/
base_func.vbs
379 lines (319 loc) · 7.7 KB
/
base_func.vbs
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
Option Explicit
Function RunCommand(cmd)
dim objsh,res
set objsh = wscript.CreateObject("WScript.Shell")
res = objsh.Run(cmd,1,true)
if res <> 0 Then
WScript.Stderr.WriteLine("run command ("& cmd &") error ("& res &")")
WScript.Quit(res)
End if
End Function
Function RemoveDir(dirname)
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(dirname)
End Function
Function GetCwd()
dim fso
set fso = WScript.CreateObject("Scripting.FileSystemObject")
GetCwd = fso.GetAbsolutePathName(".")
set fso = Nothing
End Function
Function GetAbsPath(path)
dim fso
set fso = WScript.CreateObject("Scripting.FileSystemObject")
GetAbsPath= fso.GetAbsolutePathName(path)
set fso = Nothing
End Function
Function GetWholePath(fname)
dim fso
set fso = WScript.CreateObject("Scripting.FileSystemObject")
GetWholePath = fso.GetAbsolutePathName(fname)
set fso = Nothing
End Function
Function CheckVariable(varname)
dim wsh,val,key
set wsh = WScript.CreateObject("WScript.Shell")
key = "%" & varname & "%"
val = wsh.ExpandEnvironmentStrings(key)
if val = key Then
wscript.stderr.write("variable (" + varname + ") not defined" & chr(13) & chr(10))
wscript.quit(4)
end if
End Function
Function GetEnv(varname)
dim wsh,val,key
set wsh = WScript.CreateObject("WScript.Shell")
key = "%" & varname & "%"
val = wsh.ExpandEnvironmentStrings(key)
if val = key Then
GetEnv=null
else
GetEnv=val
end if
End Function
Function GetTempName(pattern)
dim fso,fname,tempdir,shell,extname,objRegEx,tempname
set fso = CreateObject("Scripting.FileSystemObject")
set shell = CreateObject("WScript.Shell")
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = "XXXXXX"
tempdir = shell.ExpandEnvironmentStrings("%TEMP%")
fname = fso.GetTempName()
extname = fso.GetBaseName(fname)
'WScript.Stderr.Writeline("extname of " & fname & "=" & extname)
tempname = objRegEx.Replace(pattern,extname)
If tempname = pattern Then
tempname = fname
End If
GetTempName=tempdir & "\" & tempname
End Function
Function WriteTempFile(str,pattern)
dim tempfile
dim fso,fh
tempfile=GetTempName(pattern)
set fso = CreateObject("Scripting.FileSystemObject")
set fh = fso.CreateTextFile(tempfile,True)
fh.Write(str)
WriteTempFile=tempfile
End Function
Function SetEnv(key,value)
dim objShell,colprocenvars
Set objShell = WScript.CreateObject("WScript.Shell")
Set colprocenvars = objShell.Environment("Process")
colprocenvars(key) = value
End Function
Function Chdir(path)
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.CurrentDirectory = path
set oShell=Nothing
End Function
Function FileExists(pathf)
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(pathf)) Then
FileExists=1
Else
FileExists=0
End If
End Function
Function RemoveFileSafe(fname)
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
If FileExists(fname) Then
fso.DeleteFile fname
End If
End Function
Function FolderExists(pathd)
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists(pathd)) Then
FolderExists=1
Else
FolderExists=0
End If
End Function
Function VersionCompare(basever,cmpver)
dim basearr,cmparr
dim curbase,curcmp
dim baselen,cmplen,maxlen
dim i
basearr = Split(basever,".")
cmparr = Split(cmpver,".")
baselen = UBound(basearr)
cmplen = UBound(cmparr)
maxlen = baselen
If cmplen > baselen Then
maxlen = cmplen
End If
For i =0 to maxlen Step 1
if i > baselen Then
curbase = 0
Else
curbase = CInt(basearr(i))
End If
If i > cmplen Then
curcmp = 0
Else
curcmp = CInt(cmparr(i))
End If
If curcmp < curbase Then
VersionCompare=false
Exit Function
End If
If curcmp > curbase Then
VersionCompare=true
Exit Function
End If
Next
VersionCompare=true
End Function
Function GetRunOut(exefile,commands,ByRef filterfunc,ByRef filterctx)
dim objshell
dim execobj
dim cmd,line,retline,retval
cmd = "cmd.exe /c " & chr(34) & exefile & chr(34) & " " & commands
set objshell = WScript.CreateObject("WScript.Shell")
set execobj = objshell.Exec(cmd)
retline = ""
retval=false
Do While Not execobj.Stdout.AtEndOfStream
line = execobj.Stdout.ReadLine()
if not retval Then
Execute("retval = " & filterfunc & "(line," & filterctx & ")")
End If
Loop
if not retval Then
line = execobj.Stdout.ReadLine()
Execute("retval = " & filterfunc & "(line," & filterctx & ")")
End If
GetRunOut=retline
End Function
Function StrHasChar(instr,ch)
dim xlen
dim i
dim curch
xlen=Len(instr)
For i=0 to xlen-1
curch=Mid(instr,i+1,1)
if curch = ch Then
StrHasChar=True
exit Function
End If
Next
StrHasChar=False
End Function
Function ReadDirAll(dir)
dim fso
dim folder
dim lists
dim files,retfiles,dirs
dim i
dim curfile
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(dir)
set files = folder.Files
set dirs = folder.SubFolders
i = 0
retfiles=""
For Each curfile in files
If i <> 0 Then
retfiles = retfiles & ";"
End If
retfiles = retfiles & curfile
i = i + 1
Next
For Each curfile in dirs
If i <> 0 Then
retfiles = retfiles & ";"
End If
retfiles = retfiles & curfile
i = i + 1
Next
ReadDirAll=retfiles
End Function
Function ReadDir(dir)
dim fso
dim folder
dim lists
dim files,retfiles,dirs
dim i
dim curfile
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(dir)
set dirs = folder.SubFolders
i = 0
retfiles=""
For Each curfile in dirs
If i <> 0 Then
retfiles = retfiles & ";"
End If
retfiles = retfiles & curfile
i = i + 1
Next
ReadDir=retfiles
End Function
Class ArrayObject
Private m_array()
Public Sub Push(item)
dim size
dim newsize
size = UBound(m_array)
newsize = size + 1
ReDim Preserve m_array(newsize)
m_array(size)=item
End Sub
Public Property Get GetItem(idx)
dim size
size = UBound(m_array)
If idx >= size Then
GetItem=null
Else
GetItem=m_array(idx)
End If
End Property
Public Property Get Size()
Size = UBound(m_array)
End Property
Private Sub Class_Initialize()
ReDim m_array(0)
End Sub
Private Sub Class_Terminate()
ReDim m_array(0)
End Sub
End Class
Class DictObject
Private m_dict
Public Sub Add(k,v)
if m_dict.Exists(k) Then
m_dict.Remove(k)
End If
m_dict.Add k,v
End Sub
Public Property Get Size()
Size = UBound(m_dict.Keys()) + 1
End Property
Public Property Get Key(idx)
dim size
size = UBound(m_dict.Keys()) + 1
if idx < size Then
Key=m_dict.Keys()(idx)
Else
Key=null
End If
End Property
Public Sub Append(key,val)
dim obj
if m_dict.Exists(key) Then
m_dict.Item(key).Push(val)
Else
set obj = new ArrayObject
obj.Push(val)
m_dict.Add key,obj
End If
End Sub
Public Sub Delete(key)
m_dict.remove(key)
End Sub
Public Property Get Exists(k)
Exists=m_dict.Exists(k)
End Property
Public Property Get Value(k)
dim obj
On Error Resume Next
Err.Clear
Value=m_dict.Item(k)
if Err.Number <> 0 Then
set Value=m_dict.Item(k)
End If
On Error Goto 0
End Property
Private Sub Class_Initialize()
set m_dict = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
set m_dict=Nothing
End Sub
End Class