-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodGeneral.bas
More file actions
151 lines (122 loc) · 3.97 KB
/
modGeneral.bas
File metadata and controls
151 lines (122 loc) · 3.97 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
Attribute VB_Name = "modGeneral"
Function ReadFile(filename)
f = FreeFile
temp = ""
Open filename For Binary As #f ' Open file.(can be text or image)
temp = Input(FileLen(filename), #f) ' Get entire Files data
Close #f
ReadFile = temp
End Function
Function GetFolderFiles(ByVal folder, Optional filter = ".*", Optional fullpath As Boolean = True) As String()
Dim fnames() As String
If Not FolderExists(folder) Then
'returns empty array if fails
GetFolderFiles = fnames()
Exit Function
End If
folder = IIf(Right(folder, 1) = "\", folder, folder & "\")
If Left(filter, 1) = "*" Then extension = Mid(filter, 2, Len(filter))
If Left(filter, 1) <> "." Then filter = "." & filter
fs = Dir(folder & "*" & filter, vbHidden Or vbNormal Or vbReadOnly Or vbSystem)
While fs <> ""
If fs <> "" Then push fnames(), IIf(fullpath, folder, "") & fs
fs = Dir()
Wend
GetFolderFiles = fnames()
End Function
Function GetBaseName(path) As String
tmp = Split(path, "\")
ub = tmp(UBound(tmp))
If InStr(1, ub, ".") > 0 Then
GetBaseName = Mid(ub, 1, InStrRev(ub, ".") - 1)
Else
GetBaseName = ub
End If
End Function
Function FileNameFromPath(fullpath) As String
If InStr(fullpath, "\") > 0 Then
tmp = Split(fullpath, "\")
FileNameFromPath = CStr(tmp(UBound(tmp)))
Else
FileNameFromPath = fullpath
End If
End Function
Function FileExists(path) As Boolean
On Error Resume Next
If Len(path) = 0 Then Exit Function
If Dir(path, vbHidden Or vbNormal Or vbReadOnly Or vbSystem) <> "" Then
If Err.Number <> 0 Then Exit Function
FileExists = True
End If
End Function
Function FolderExists(path) As Boolean
If Len(path) = 0 Then Exit Function
If Dir(path, vbDirectory) <> "" Then FolderExists = True _
Else FolderExists = False
End Function
Function GetFreeFileName(folder, Optional extension = ".txt") As String
If Not FolderExists(folder) Then Exit Function
If Right(folder, 1) <> "\" Then folder = folder & "\"
If Left(extension, 1) <> "." Then extension = "." & extension
Dim tmp As String
Do
tmp = folder & RandomNum() & extension
Loop Until Not FileExists(tmp)
GetFreeFileName = tmp
End Function
Function RandomNum() As Long
Dim tmp As Long
Dim tries As Long
On Error Resume Next
Do While 1
Err.Clear
Randomize
tmp = Round(Timer * Now * Rnd(), 0)
RandomNum = tmp
If Err.Number = 0 Then Exit Function
If tries < 100 Then
tries = tries + 1
Else
Exit Do
End If
Loop
RandomNum = GetTickCount
End Function
Function GetMySetting(key, def)
GetMySetting = GetSetting(App.EXEName, "Settings", key, def)
End Function
Sub SaveMySetting(key, Value)
SaveSetting App.EXEName, "Settings", key, Value
End Sub
Sub push(ary, Value) 'this modifies parent ary object
On Error GoTo Init
x = UBound(ary) '<-throws Error If Not initalized
ReDim Preserve ary(UBound(ary) + 1)
ary(UBound(ary)) = Value
Exit Sub
Init: ReDim ary(0): ary(0) = Value
End Sub
Function AryIsEmpty(ary) As Boolean
On Error GoTo oops
x = UBound(ary)
AryIsEmpty = False
Exit Function
oops: AryIsEmpty = True
End Function
Sub FormPos(fform As Form, Optional andSize As Boolean = False, Optional save_mode As Boolean = False)
On Error Resume Next
Dim f, sz
f = Split(",Left,Top,Height,Width", ",")
If fform.WindowState = vbMinimized Then Exit Sub
If andSize = False Then sz = 2 Else sz = 4
For i = 1 To sz
If save_mode Then
ff = CallByName(fform, f(i), VbGet)
SaveSetting App.EXEName, fform.name & ".FormPos", f(i), ff
Else
def = CallByName(fform, f(i), VbGet)
ff = GetSetting(App.EXEName, fform.name & ".FormPos", f(i), def)
CallByName fform, f(i), VbLet, ff
End If
Next
End Sub