overriding .ini file values

Hi,
I have an Records.ini that has 100(fixed, always 100) values.
Now I have a string that pass
eg. Main.Box.Eqt2.Eqt21.aiiBox.Value = 100
how can i override the old value in the Records.ini file?

Attached original code of writing into .ini file and Records.ini file

*Note: i am using vb script.
Records.ini
csharp_learnerAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
Okay, here's about the simplest I could get it, and it makes a few assumptions:

- there are no spaces to the left of the lines in the INI
- there are no keys that are supstrings of other keys, like
  ... key_name_1
  ... key_name_100

Hope this helps.

UpdateIni "records.ini", "Main.Box.Eqt2.Eqt21.aiiBox.Value", "100"

Function UpdateIni(sFile, sKey, sValue)
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oIniFile = oFSO.OpenTextFile(sFile, 1, False, -2)
    sLine = Split(oIniFile.ReadAll, VbCrLf)
    oIniFile.Close
    For i = 0 To UBound(sLine)-1
        If Left(sLine(i), Len(sKey)) = sKey Then
          sLine(i) = sKey & " = " & sValue
        End If
    Next
    Set oIniFile = oFSO.OpenTextFile(sFile, 2, True)
    oIniFile.Write(Join(sLine, vbCrLf))
    oIniFile.Close
    Set oIniFile = Nothing
    Set oFSO = Nothing
End Function

Open in new window

~bp
0
 
csharp_learnerAuthor Commented:

...
Call Write_in_Records("C:\Records.ini", newString)

Next

End Sub

Function Write_in_Records(File, Content)
        Dim newFS
        Dim File_Name
            Set newFS = CreateObject("Scripting.FileSystemObject")
            Set File_Name = newFS.OpenTextFile(File, 8, True)
            File_Name.Write Content & vbCrLf
            File_Name.Close
            Set File_Name = Nothing
            Set newFS = Nothing


End Function

Open in new window

0
 
Bill PrewCommented:
So, you want to update the INI file on disk, and change a certain value that it already contains?

~bp
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
csharp_learnerAuthor Commented:
Yes, I want to update the fields.
0
 
Bill PrewCommented:
A bit overkill for this but I had provided a good general purpose INI file update function in the following question:

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26661747.html

Using that routine you could just do this in your main code:

WriteIni( "records.ini", "Records", "Main.Box.Eqt2.Eqt21.aiiBox.Value", "100" )

Open in new window

~bp
0
 
csharp_learnerAuthor Commented:
Thank you billprew for your reply.

Unfortunately this is really an overkill for my program.
As there are aleready alot of things happening in my program, I was hoping to get this function done as specific and as simple as possible...
0
 
csharp_learnerAuthor Commented:
Just what i was looking for, thanks.
0
 
Bill PrewCommented:
Glad that was useful, thanks.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.