Skip to content

Commit d17a5e0

Browse files
committed
x
1 parent 6f031c0 commit d17a5e0

File tree

4 files changed

+412
-162
lines changed

4 files changed

+412
-162
lines changed

MiClsINI.vbs MiClassINI.vbs

+160-160
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,161 @@
1-
Option explicit
2-
' funciona !
3-
' mas rapido solo claves a recordset para ordenar
4-
' enumeracione todas las claves de una seccion?
5-
' como no perder comentarios?
6-
' variable de estado : cargado, sucio
7-
' ahora es sensible a caja, deberia no serlo?
8-
9-
dim mini
10-
set mini=new Clsini
11-
mini.OpenINIFile(Scriptpath()&"sample.ini")
12-
wscript.echo mini.GetINIValue("Sec2","value1")
13-
wscript.echo mini.GetINIValue("pepe","juan")
14-
wscript.echo mini.writeinivalue("Sec99","mivalor","3.141592")
15-
mini.CloseINIFile()
16-
17-
Function ScriptPath()
18-
Dim path: path = WScript.ScriptFullName
19-
ScriptPath = Left(path, InStrRev(path, "\"))
20-
End Function
21-
22-
23-
' ____________________ START INI Class HERE ________________________________________
24-
25-
Class ClsINI
26-
'pierde los comentarios en el ini
27-
'reordena los campos al salvar
28-
29-
Private FSO, TS, Dic, sFil,dirty
30-
31-
Private Sub Class_Initialize()
32-
Set FSO = CreateObject("Scripting.FileSystemObject")
33-
End Sub
34-
35-
Private Sub Class_Terminate()
36-
Set FSO = Nothing
37-
Set Dic = Nothing
38-
End Sub
39-
40-
'--Function to Read INI file into Dic: -------------------------------------
41-
Public Function OpenINIFile(sFilePath)
42-
Dim s, sSec, sList
43-
If FSO.FileExists(sFilePath) = False Then
44-
OpenINIFile = False
45-
Exit Function
46-
End If
47-
sFil = sFilePath
48-
Set Dic = Nothing '-- reset Dic in Case an earlier file wasn't closed with CloseINIFile.
49-
50-
Set Dic = CreateObject("Scripting.Dictionary")
51-
dim pref:pref= "[]"
52-
dim equ
53-
On Error Resume Next
54-
Set TS = FSO.OpenTextFile(sFil, 1)
55-
Do While TS.AtEndOfStream = False
56-
s = Trim(TS.ReadLine)
57-
If Len(s) > 0 Then
58-
equ=instr(s,"=")
59-
If left(s,1)=";" then
60-
'comentario, saltamos linea
61-
elseIf Left(s, 1) = "[" Then
62-
pref=s
63-
ElseIf equ>1 Then
64-
Dic.Item (pref&" "&left(s,equ-1))= trim(mid(s,equ+1))
65-
end if
66-
End If
67-
Loop
68-
TS.Close
69-
Set TS = Nothing
70-
OpenINIFile = True
71-
End Function
72-
73-
'-------------------------------------------------------------------------
74-
75-
Public Sub CloseINIFile()
76-
WriteNewINI()
77-
Set Dic = Nothing
78-
End Sub
79-
80-
'-------------------------------------------------------------------------
81-
82-
'read one value from INI. return 0 on success. 1 If no such value. 2 If no such section.
83-
' 3 If no file open. 4 If unexpected error in text of file.
84-
Public Function GetINIValue(sSection, sKey)
85-
Dim s1: s1 = "["& sSection &"] "& sKey
86-
if Dic is Nothing then GetINIValue = Null: Exit Function
87-
88-
if not dic.exists(s1) then GetINIValue = Null:exit function
89-
GetINIValue = Dic.Item(s1)
90-
End Function
91-
92-
93-
94-
'--------- Write INI value: ---------------------------------
95-
' return 0 on success. 2 If no such section.
96-
' 3 If no file open. 4 If unexpected error in text of file.
97-
Public Function WriteINIValue(sSection, sKey, sValue)
98-
if Dic is Nothing then WriteINIValue = 3: Exit Function
99-
Dim s1:s1 = "["& sSection &"] "& sKey
100-
Dic(s1)=sValue
101-
dirty=1
102-
WriteINIValue=0
103-
end function
104-
105-
'---Function to delete single key=value pair: ---------------------------------------
106-
107-
Public Function DeleteINIValue(sSection, sKey)
108-
Dim s1:s1 = "["& sSection &"] "& sKey
109-
if Dic is Nothing then DeleteINIValue = 3: Exit Function
110-
if not dic.exists(s1) then DeleteINIValue = 2:exit function
111-
dic.remove(s1)
112-
DeleteINIValue =0
113-
End Function
114-
115-
'-----------------------------------------------------------
116-
Private Sub WriteNewINI() 'ordenar y salvar
117-
const advarchar=200
118-
const adopenstatic=3
119-
Const fsoForWriting = 2
120-
dim i,s1,k1,n,rs,lastkey,sk
121-
if dirty=0 then exit sub
122-
Set rs = CreateObject("ADODB.RECORDSET")
123-
with rs
124-
125-
.fields.append "SectionKey", adVarChar, 100
126-
127-
.CursorType = adOpenStatic
128-
.open
129-
for each i in Dic.keys
130-
.AddNew
131-
rs("SectionKey").Value = i
132-
.Update
133-
next
134-
.Sort= " SectionKey ASC"
135-
136-
.MoveFirst
137-
lastkey="[]"
138-
Set TS = FSO.OpenTextFile(sFil, fsoForWriting)
139-
do while not rs.EOF
140-
sk=rs("SectionKey")
141-
wscript.echo ">" & sk & "<"
142-
n=instr(sk,"]")
143-
s1=left(sk,n)
144-
k1=trim(mid(sk,n+1))
145-
if s1<>lastkey then
146-
lastkey=s1
147-
ts.writeline
148-
ts.writeline lastkey
149-
end if
150-
ts.writeline k1 & "=" & Dic(sk)
151-
.movenext
152-
loop
153-
.close
154-
end with
155-
set rs=Nothing
156-
ts.close
157-
set ts=Nothing
158-
end sub
159-
end class
160-
1+
Option explicit
2+
' funciona !
3+
' mas rapido solo claves a recordset para ordenar
4+
' enumeracione todas las claves de una seccion?
5+
' como no perder comentarios?
6+
' variable de estado : cargado, sucio
7+
' ahora es sensible a caja, deberia no serlo?
8+
9+
dim mini
10+
set mini=new Clsini
11+
mini.OpenINIFile(Scriptpath()&"sample.ini")
12+
wscript.echo mini.GetINIValue("Sec2","value1")
13+
wscript.echo mini.GetINIValue("pepe","juan")
14+
wscript.echo mini.writeinivalue("Sec99","mivalor","3.141592")
15+
mini.CloseINIFile()
16+
17+
Function ScriptPath()
18+
Dim path: path = WScript.ScriptFullName
19+
ScriptPath = Left(path, InStrRev(path, "\"))
20+
End Function
21+
22+
23+
' ____________________ START INI Class HERE ________________________________________
24+
25+
Class ClsINI
26+
'pierde los comentarios en el ini
27+
'reordena los campos al salvar
28+
29+
Private FSO, TS, Dic, sFil,dirty
30+
31+
Private Sub Class_Initialize()
32+
Set FSO = CreateObject("Scripting.FileSystemObject")
33+
End Sub
34+
35+
Private Sub Class_Terminate()
36+
Set FSO = Nothing
37+
Set Dic = Nothing
38+
End Sub
39+
40+
'--Function to Read INI file into Dic: -------------------------------------
41+
Public Function OpenINIFile(sFilePath)
42+
Dim s, sSec, sList
43+
If FSO.FileExists(sFilePath) = False Then
44+
OpenINIFile = False
45+
Exit Function
46+
End If
47+
sFil = sFilePath
48+
Set Dic = Nothing '-- reset Dic in Case an earlier file wasn't closed with CloseINIFile.
49+
50+
Set Dic = CreateObject("Scripting.Dictionary")
51+
dim pref:pref= "[]"
52+
dim equ
53+
On Error Resume Next
54+
Set TS = FSO.OpenTextFile(sFil, 1)
55+
Do While TS.AtEndOfStream = False
56+
s = Trim(TS.ReadLine)
57+
If Len(s) > 0 Then
58+
equ=instr(s,"=")
59+
If left(s,1)=";" then
60+
'comentario, saltamos linea
61+
elseIf Left(s, 1) = "[" Then
62+
pref=s
63+
ElseIf equ>1 Then
64+
Dic.Item (pref&" "&left(s,equ-1))= trim(mid(s,equ+1))
65+
end if
66+
End If
67+
Loop
68+
TS.Close
69+
Set TS = Nothing
70+
OpenINIFile = True
71+
End Function
72+
73+
'-------------------------------------------------------------------------
74+
75+
Public Sub CloseINIFile()
76+
WriteNewINI()
77+
Set Dic = Nothing
78+
End Sub
79+
80+
'-------------------------------------------------------------------------
81+
82+
'read one value from INI. return 0 on success. 1 If no such value. 2 If no such section.
83+
' 3 If no file open. 4 If unexpected error in text of file.
84+
Public Function GetINIValue(sSection, sKey)
85+
Dim s1: s1 = "["& sSection &"] "& sKey
86+
if Dic is Nothing then GetINIValue = Null: Exit Function
87+
88+
if not dic.exists(s1) then GetINIValue = Null:exit function
89+
GetINIValue = Dic.Item(s1)
90+
End Function
91+
92+
93+
94+
'--------- Write INI value: ---------------------------------
95+
' return 0 on success. 2 If no such section.
96+
' 3 If no file open. 4 If unexpected error in text of file.
97+
Public Function WriteINIValue(sSection, sKey, sValue)
98+
if Dic is Nothing then WriteINIValue = 3: Exit Function
99+
Dim s1:s1 = "["& sSection &"] "& sKey
100+
Dic(s1)=sValue
101+
dirty=1
102+
WriteINIValue=0
103+
end function
104+
105+
'---Function to delete single key=value pair: ---------------------------------------
106+
107+
Public Function DeleteINIValue(sSection, sKey)
108+
Dim s1:s1 = "["& sSection &"] "& sKey
109+
if Dic is Nothing then DeleteINIValue = 3: Exit Function
110+
if not dic.exists(s1) then DeleteINIValue = 2:exit function
111+
dic.remove(s1)
112+
DeleteINIValue =0
113+
End Function
114+
115+
'-----------------------------------------------------------
116+
Private Sub WriteNewINI() 'ordenar y salvar
117+
const advarchar=200
118+
const adopenstatic=3
119+
Const fsoForWriting = 2
120+
dim i,s1,k1,n,rs,lastkey,sk
121+
if dirty=0 then exit sub
122+
Set rs = CreateObject("ADODB.RECORDSET")
123+
with rs
124+
125+
.fields.append "SectionKey", adVarChar, 100
126+
127+
.CursorType = adOpenStatic
128+
.open
129+
for each i in Dic.keys
130+
.AddNew
131+
rs("SectionKey").Value = i
132+
.Update
133+
next
134+
.Sort= " SectionKey ASC"
135+
136+
.MoveFirst
137+
lastkey="[]"
138+
Set TS = FSO.OpenTextFile(sFil, fsoForWriting)
139+
do while not rs.EOF
140+
sk=rs("SectionKey")
141+
wscript.echo ">" & sk & "<"
142+
n=instr(sk,"]")
143+
s1=left(sk,n)
144+
k1=trim(mid(sk,n+1))
145+
if s1<>lastkey then
146+
lastkey=s1
147+
ts.writeline
148+
ts.writeline lastkey
149+
end if
150+
ts.writeline k1 & "=" & Dic(sk)
151+
.movenext
152+
loop
153+
.close
154+
end with
155+
set rs=Nothing
156+
ts.close
157+
set ts=Nothing
158+
end sub
159+
end class
160+
161161
' __________________ End INI Class HERE ______________________________

console_menu console_menu.vbs

File renamed without changes.

index.txt

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
lista_recordset: uses odbc to read a csv to a disconnected recordset, then uses an array to tell a sub how to print it in columns.
33

4-
mi_ClsIni :
4+
mi_ClassIni :
55
An exercise in using classes. It reads an ini file to a dictionnary making items of the ini available to program. The progam can add, modify and delete items. At the end the class allows to save back the values to the ini. Warning: If the ini file is written back it loses the comment lines!
66

77
my_userinput :
@@ -10,4 +10,12 @@ function that gets an array defining an input form with text /radio buttons/chec
1010
console_menu :
1111
A "foolproof" console menu. The accepted inputs are defined as constants. The menu text and the switchboard use these constants. The input validation find the characters in the menu text yhet's built from the constants.
1212

13-
general_lib : Assorted general use routines
13+
general_lib :
14+
Assorted general use routines
15+
16+
sunrise_final:
17+
Calculates sunrise and sunset time in civil time or any position under the polar cicles. Has an angle compensation for if the place is not at the same height as the visible horizon (mountins, top of a tower)
18+
Gives results +/- 1 min of the results provides by the US Naval Observatory site.
19+
20+
21+

0 commit comments

Comments
 (0)