/build/static/layout/Breadcrumb_cap_w.png

VBScript to modify already installed ini file

Hi,

I'm currently packaging Lotus Notes version 8.51. As part of the installation sequence I need to add 3 ini file entries to an existing Notes.ini file that will be present for users on their homedrive. The actual location for the ini file is; O:\Lotus\Notes\Data.

My script needs to add the following 3 lines to an existing ini file;

UseBasicNotes=1
Create_R85_Databases=1
AttachmentActionDefault=1

Has anybody had any experience with writing VBScripts that can do something similar to the above?

Regards,
Cowley

0 Comments   [ + ] Show comments

Answers (6)

Posted by: anonymous_9363 14 years ago
Red Belt
0
Go to JSWare's site and download the class pack. There's an INI file class in it which, once you've used it, will wonder forever why anyone uses their own line-by-line loopsto get to a section, then more loops to read values and so on.

Don't be put off by the scary word "class": it's just a library of ready-rolled functions, really and there's example code within the class file.
Posted by: cowley 14 years ago
Orange Belt
0
VBSCab,

Thanks for the link, I've download the sample clsini & associated VBS file from the site, I don't have a clue what it's supposed to do, even after reading the help file it still doesn't make sense to me.

What is it supposed to do?
Posted by: cowley 14 years ago
Orange Belt
0
Also, is it possible to amend an existing ini file present on the machine via the IniFile table in your MSI?
Posted by: mekaywe 14 years ago
Brown Belt
0
Yes, you can append the INI file which is existing on the machine if that INI file is present in INI table in MSI
To update/append INI file with new entries you can use Wise Script and also INI Table in MSI
Posted by: anonymous_9363 14 years ago
Red Belt
0
What is it supposed to do?I think the usage is pretty self-explanatory, TBH, but I'm feeling generous this evening. Here's some code for you which should work right out of the box to set ONE of the values. I've called the section containing the value/data pairs "Unknown" since you don't say what the actual section name is. I'll leave it to you to change that and to add the code to write the other values.Option Explicit

Dim strMsg
Dim objINIFile
Dim strINIPath
Dim strINIFileName
Dim strINIFile
Dim strINISection
Dim strINIValue
Dim strINIData
Dim intResult

Set objINIFile = New ClsINI
If Not IsObject(objINIFile) Then
WScript.Echo "Unable to create INI file object. Cannot proceed."
WScript.Quit(False)
End If

strINIPath = "O:\Lotus\Notes\Data"
strINIFileName = "NOTES.INI"
strINIFile = strINIPath & "\" & strINIFileName

'// You should add some tests here using FileSystemObject object
'// to determine if drive, folder tree and file exist before proceeding.

strINISection = "Unknown"
strINIValue = "UseBasicNotes"
strINIData = "1"

If objINIFile.OpenINIFile(strINIFile) = False Then
WScript.Echo "Unable to open INI file. Cannot proceed."
Set objINIFile = Nothing
WScript.Quit(False)
End If

'// Write a new value:
intResult = objINIFile.WriteINIValue(strINISection, strINIValue, strINIData)
strMsg = "Wrote new value '" & strINIValue & "'"
strMsg = strMsg & " with data ' & strINIData & "'"
strMsg = strMsg & " to section ' & strINISection & "'"
strMsg = strMsg & vbCRLF
strMsg = strMsg & "Return value was ' & intResult & "'"

WScript.Echo strMsg

Set objINIFile = Nothing

'--//////////////// Class ClsINIOps /////////////////////////////////////////////--
' To work with an INI file, first 'open' it. This Function reads the INI file into a dictionary
' object. The Class is Then ready For operations. The file itself is Not open but as long as
' OpenINIFile has been successfully called the Class will hold the file in memory as a
' dictionary object and any number of operations can be performed.

' Ret = OpenINIFile(FilePath) [ Boolean - returns True If file opened successfully. ]

' CloseINIFile() [ Sub to close INI file after use. This sets the dictionary to Nothing.
' The INI file itself is Not actually open so CloseINIFile is Not strictly
' required. If another file is opened the dictionary will be Set to Nothing
' before proceeding. If the Class is Set to Nothing the dictionary will also be cleared.

'---- Get Section Names: ------------------------------
' Ret = GetSectionNames()
' ex.: r = Cls.GetSectionNames()
' Return: If no file is open or file has no sections, an array is returned
' with ubound(0) and the value of array(0) is ""
' If there are sections in the file the names are returned in an array.

'------ Get INI value: -------------------------------
' Ret = GetINIValue(Section, Key, Value)
' ex.: r = Cls.GetINIValue("Section1", "val4", s)
' on success Ret returns 0 and s returns value of Key.
' Errors: 1 - no such key. 2 - no such section. 3 - no file open. 4 - unexpected error in text of file.

'------ Get Section values: ------------------------
' Ret = GetSectionValues(sSection, sArray)
' ex.: r = Cls.GetSectionValues("Section1", A1)
' If r = 0 Then ......(A1 holds array of key=value pairs. )
' Return: 0 - success. 2 - no such section. 3 - no file open.

'---- Write INI value: -----------------------------
' Ret = WriteINIValue(Section, Key, Value)
' ex.: r = Cls.WriteINIValue("Section1", "backcolor", "blue")
' Sets value: backcolor=blue Key and Section will be created If necessary.
' Return: 0 - success. 3 - no file open. 4 - unexpected error in text of file.

'------ Delete INI value: ------------------------
' Ret = DeleteINIValue(Section, Key)
' ex.: r = Cls.DeleteINIValue("Section1", "backcolor")
' Deletes 'backcolor' key.
' Return: 0 - success. 1 - no such key. 2 - no such section. 3 - no file open. 4 - unexpected error in text of file.
' For this Function success would be If Ret < 3.

'----- Write INI section: ------------------------
' Ret = WriteINISection(Section)
' ex.: r = Cls.WriteINISection("Section1")
' writes a new section to INI file.
' Return: 0 - success. 2 - section already exists. 3 - no file open.
' For this Function success would be If Ret < 3.

'---- Delete INI section: -------------------------
' Ret = DeleteINISection(Section)
' ex.: r = Cls.DeleteINISection("Section1")
' Return: 0 - success. 2 - no such section. 3 - no file open.
' For this Function success would be If Ret < 3.

'-- NOTE ABOUT ERROR CODES: The error codes are designed to relate
'--to the same thing regardless of the Function. 0 is always success.
'-- 1 always means the key does Not exist, regardless of whether you're
'--trying to read from or delete the key.
'-- 2 always relates to the section. For functions that require a section
'-- it means the section does Not exist. When trying to write a new section
'-- it means the section already exists.
'-- 3 always means that no file has been opened.
'-- 4 is an unknown error indicating that there was an unexpected problem.

' ____________________ START INI Class HERE ________________________________________

Class ClsINI
Private FSO, TS, Dic, sFil, sBullet

Private Sub Class_Initialize()
Set FSO = CreateObject("Scripting.FileSystemObject")
sBullet = Chr(149)
End Sub

Private Sub Class_Terminate()
Set FSO = Nothing
Set Dic = Nothing
End Sub

'--Function to Read INI file into Dic: -------------------------------------
Public Function OpenINIFile(sFilePath)
Dim s, sSec, sList
If FSO.FileExists(sFilePath) = False Then
OpenINIFile = False
Exit Function
End If
sFil = sFilePath
Set Dic = Nothing '-- reset Dic in Case an earlier file wasn't closed with CloseINIFile.


'--Read INI file into dictionary object. Each section will be a key.
'--section key=value pairs will be added as Dic.key item.
'-- After file is read there will be one Dic.Key For Each section header.
'-- Each Key Item will be a string of key=value pairs separated by bullet characters.

Set Dic = CreateObject("Scripting.Dictionary")
Dic.Add "Glo_bal", sBullet
On Error Resume Next
Set TS = FSO.OpenTextFile(sFil, 1)
Do While TS.AtEndOfStream = False
s = TS.ReadLine
s = Trim(s)
If Len(s) > 0 Then
If Left(s, 1) = "[" Then
sSec = s
Dic.Add sSec, sBullet
ElseIf Dic.Exists(sSec) Then
sList = Dic.Item(sSec)
sList = sList & s & sBullet
Dic.Item(sSec) = sList
Else '--global comment at top. If no Dic.Key sSec exists Then no sections have been read yet.
sList = Dic.Item("Glo_bal")
sList = sList & s & sBullet
Dic.Item("Glo_bal") = sList
End If
End If
Loop
TS.Close
Set TS = Nothing
OpenINIFile = True
End Function

'-------------------------------------------------------------------------
'--Close an open INI file. The file is Not actually open but it's closed
'--For the purposes of this Class by setting Dic = Nothing.

Public Sub CloseINIFile()
Set Dic = Nothing
End Sub

'-------------------------------------------------------------------------
'-- Get list of INI section names. Returns an array of names. If no file open
'-- or no sections in file Then an array of ubound(0) is returned with an array(0)
'-- value of "".
Public Function GetSectionNames()
Dim ASec, iK, sTemp, sTemp1
If IsObject(Dic) = False Then
ASec = array("")
GetSectionNames = ASec '-- no file open.
Exit Function
End If

If Dic.Count = 0 Then
ASec = array("")
GetSectionNames = ASec '-- no keys.
Exit Function
End If

On Error Resume Next
ASec = Dic.Keys
For iK = 0 to UBound(ASec)
sTemp = ASec(iK)
If (sTemp <> "Glo_bal") Then
sTemp = Mid(sTemp, 2, (len(sTemp) - 2))
sTemp1 = sTemp1 & (sTemp & sBullet)
End If
Next
GetSectionNames = Split(sTemp1, sBullet)
End Function
'-------------------------------------------------------------------------
'read one value from INI. return 0 on success. 1 If no such value. 2 If no such section.
' 3 If no file open. 4 If unexpected error in text of file.
Public Function GetINIValue(sSection, sKey, sValue)
Dim sList, pt, pt2, s1, Lens1

Select Case CheckBasics(sSection)
Case 3
GetINIValue = 3 '-- return 3: no file open.
Exit Function
Case 2
GetINIValue = 2 '-- return 2: no such section.
Exit Function
End Select

sValue = ""
sList = Dic.Item(sSection)
s1 = sBullet & sKey & "="
Lens1 = Len(s1)

pt = InStr(1, sList, s1, 1)
If pt = 0 Then
sValue = ""
GetINIValue = 1 '-- return 1: no such key.
Exit Function
End If

pt2 = InStr((pt + Lens1), sList, sBullet, 1)
If pt2 = 0 Then
GetINIValue = 4 '--error
ElseIf pt2 = (pt + 1) Then '-- key exists, value is ""
GetINIValue = 0
Else
sValue = Mid(sList, (pt + Lens1), (pt2 - (pt + Lens1)))
GetINIValue = 0
End If
End Function

'------Read a Section: ------------------------------------
Public Function GetSectionValues(byVal sSection, sArray)
Dim ATemp, ATemp2(), i, i2
Select Case CheckBasics(sSection)
Case 3
GetSectionValues = 3 '-- return 3: no file open.
Exit Function
Case 2
GetSectionValues = 2 '-- return 2: no such section.
Exit Function
End Select

ATemp = Split(Dic.Item(sSection), sBullet)

'-- go through ATemp, weeding out comments.
'-- any non-comment can be added to ATemp2:

i2 = 0
For i = 0 to UBound(ATemp)
If Left(ATemp(i), 1) <> ";" Then
ReDim preserve ATemp2(i2)
ATemp2(i2) = ATemp(i)
i2 = (i2 + 1)
End If
Next

sArray = ATemp2
GetSectionValues = 0
End Function

'--------- Write INI value: ---------------------------------
' return 0 on success. 2 If no such section.
' 3 If no file open. 4 If unexpected error in text of file.

Public Function WriteINIValue(sSection, sKey, sValue)
Dim sList, pt, pt2, s1, Lens1, sSec
Select Case CheckBasics(sSection)
Case 3
WriteINIValue = 3 '-- return 3: no file open.
Exit Function
Case 2
'--If section does Not exist, write section and key=value, Then quit:
sList = sBullet & sKey & "=" & sValue & sBullet
Dic.Add sSection, sList
Call WriteNewINI
WriteINIValue = 0
Exit Function
End Select

'--section exists. Get section values:

sList = Dic.Item(sSection)
s1 = sBullet & sKey & "="
Lens1 = Len(s1)
pt = instr(1, sList, s1, 1)
'--If sKey does Not already exist, write it and quit:
If pt = 0 Then
sList = sList & sKey & "=" & sValue & sBullet
Dic.Item(sSection) = sList
Call WriteNewINI
WriteINIValue = 0
Else
'--sKey does exist. Snip out existing value from sList and rebuild string, adding new value:

pt2 = instr((pt + Lens1), sList, sBullet)
If pt2 <> 0 Then
If (pt2 + 1) < Len(sList) Then '--If Not last value in section, Get left up to "=" & value & right from bullet:
sList = (Left(sList, ((pt + Lens1) - 1))) & sValue & (Right(sList, (Len(sList) - (pt2 - 1))))
Else '--last value in section:
sList = (Left(sList, ((pt + Lens1) - 1))) & sValue & sBullet
End If
Dic.Item(sSection) = sList
Call WriteNewINI
WriteINIValue = 0
Else
WriteINIValue = 4 '--error
Exit Function
End If
End If
End Function

'---Function to delete single key=value pair: ---------------------------------------

Public Function DeleteINIValue(sSection, sKey)
Dim sSec, sList, pt, pt2, s1
Select Case CheckBasics(sSection)
Case 3
DeleteINIValue = 3 '-- return 3: no file open.
Exit Function
Case 2
DeleteINIValue = 2 '-- return 2: no such section.
Exit Function
End Select

sList = Dic.Item(sSection)
s1 = sBullet & sKey & "="
pt = InStr(1, sList, s1, 1)
If pt = 0 Then
DeleteINIValue = 1 '--return 1: no such key.
Exit Function
End If

pt2 = InStr((pt + 1), sList, sBullet, 1)
If pt2 = 0 Then
DeleteINIValue = 4 '--error.
Exit Function
Else
sList = (Left(sList, (pt))) & (Right(sList, (len(sList) - pt2)))
Dic.Item(sSection) = sList
Call WriteNewINI
DeleteINIValue = 0
End If
End Function

'------------- Function to Write new section -------------------------
Public Function WriteINISection(sSection)
Select Case CheckBasics(sSection)
Case 3
WriteINISection = 3 '-- return 3: no file open.
Case 2
Dic.Add sSection, sBullet
Call WriteNewINI
WriteINISection = 0
Case Else
WriteINISection = 2 '-- section already exists.
End Select
End Function

'---------- Function to delete section -------------------
'-- 0 - success. 2 - no such section exists. 3 - no file open.
Public Function DeleteINISection(sSection)

Select Case CheckBasics(sSection)
Case 3
DeleteINISection = 3 '-- return 3: no file open.
Case 2
DeleteINISection = 2 '-- return 2: no such section.
Case Else
Dic.Remove sSection
Call WriteNewINI
DeleteINISection = 0
End Select

End Function

'-------- Sub to update INI file after writing new value or adding section. --------
'----this Sub is called internally. It won't be called unless a file is "open".
'-- in that Case file path is valid and Dic is Not Nothing, so just Set attributes to 0
'-- and write new INI file.
'-- NOTE: This has Not been tested with system INIs under Windows versions with
'-- system file protection.

Private Sub WriteNewINI()
Dim Att, oFil, A1, i, s, s1, s2, sG
On Error Resume Next
'--remove attributes such as readonly and save current attributes:
Set oFil = FSO.GetFile(sFil)
Att = oFil.Attributes
oFil.Attributes = 0
Set oFil = Nothing

A1 = Dic.Keys
For i = 0 to UBound(A1)
s1 = A1(i)
If s1 = "Glo_bal" Then
sG = Dic.Item(s1)
sG = Replace(sG, sBullet, vbcrlf)
sG = sG & vbcrlf
Else
s2 = Dic.Item(s1)
s2 = Replace(s2, sBullet, vbcrlf)
s = s & s1 & s2 & vbcrlf
End If
Next

s = sG & s '--add in global comments section.

FSO.DeleteFile sFil, True

Set TS = FSO.CreateTextFile(sFil)
TS.Write s
TS.Close
Set TS = Nothing

Set oFil = FSO.GetFile(sFil)
oFil.Attributes = Att
Set oFil = Nothing

End Sub

'--this is just a minor Function to check For Dic instantiation and section existence.
'-- it also does the work of putting [ ] around the section name sent in.
'-- it's here to save a few lines; so these functions don't have to be re-written
'-- For Each method in the Class

Private Function CheckBasics(sSection)

If IsObject(Dic) = False Then
CheckBasics = 3 '-- return 3: no file open.
Exit Function
End If

If (Left(sSection, 1) <> "[") Then sSection = "[" & sSection
If (right(sSection, 1) <> "]") Then sSection = sSection & "]"

If Dic.Exists(sSection) = False Then
CheckBasics = 2
Else
CheckBasics = 0
End If

End Function

End Class
Posted by: shinrage 13 years ago
Senior Yellow Belt
0
really nice script, thanks!
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ