-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileUtils.bas
More file actions
74 lines (56 loc) · 2.65 KB
/
FileUtils.bas
File metadata and controls
74 lines (56 loc) · 2.65 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
Attribute VB_Name = "FileUtils"
Option Explicit
' Inside the VBE, Go to Tools -> References, then Select "Microsoft Scripting Runtime"
' (NOTE: this is required as here we do early binding, (for IntelliSense), not late/lazy binding)
' #############################################################################
Public Const krakenCredentialsFilename As String = "kraken.key"
' #############################################################################
' #############################################################################
Public Function GetDefaultKrakenCredentialsFilepath() As String
Debug.Print "Working dir", ActiveWorkbook.path
GetDefaultKrakenCredentialsFilepath = ActiveWorkbook.path & "\" & krakenCredentialsFilename
End Function
Public Function ExistsKrakenCredentialsFile(Optional ByVal filename As String = "") As Boolean
Dim fio As New FileSystemObject
If ExcelUtils.IsStringEmpty(filename) Then
filename = GetDefaultKrakenCredentialsFilepath()
End If
ExistsKrakenCredentialsFile = fio.FileExists(filename)
End Function
Public Function LoadKrakenCredentials(Optional ByVal filename As String = "") As Dictionary
Dim fio As New FileSystemObject
Dim tStream As TextStream
Dim line As String, parts() As String
Dim sKey, sValue As String
Dim creds As New Dictionary
If ExcelUtils.IsStringEmpty(filename) Then
filename = GetDefaultKrakenCredentialsFilepath()
End If
' Open with default options
' https://docs.microsoft.com/de-de/office/vba/language/reference/user-interface-help/opentextfile-method
Set tStream = fio.OpenTextFile(filename)
With tStream
Do While Not .AtEndOfStream
line = .ReadLine
If ExcelUtils.IsStringEmpty(line) Then
' Skip, empty line
ElseIf ExcelUtils.StartsWith(line, ";") Then
' Skip, comment line
Else
parts = Split(line, "=", 2)
sKey = Trim(parts(0))
sValue = Trim(parts(1))
If creds.Exists(sKey) Then
Debug.Print "Key """ & sKey & """ already exists! Overwrite with newer value ..."
creds.Remove (sKey)
End If
creds.Add sKey, sValue
End If
Loop
.Close
End With
' Debug.Print "Creds", WebUtils.BeautifyJson(creds)
Set LoadKrakenCredentials = creds
End Function
' #############################################################################
' #############################################################################