?
Solved

overriding .ini file values

Posted on 2011-10-12
8
Medium Priority
?
301 Views
Last Modified: 2012-06-22
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
0
Comment
Question by:csharp_learner
  • 4
  • 4
8 Comments
 

Author Comment

by:csharp_learner
ID: 36955163

...
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36955173
So, you want to update the INI file on disk, and change a certain value that it already contains?

~bp
0
 

Author Comment

by:csharp_learner
ID: 36955285
Yes, I want to update the fields.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 59

Expert Comment

by:Bill Prew
ID: 36955294
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
 

Author Comment

by:csharp_learner
ID: 36956256
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
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 36957725
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
 

Author Closing Comment

by:csharp_learner
ID: 36966028
Just what i was looking for, thanks.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36966043
Glad that was useful, thanks.

~bp
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question