How to modify specific lines in a .ini file?

J

Jim

Hello,

When writing multiple values to a .ini file I'm familiar with the
following basic code:

Open "c:\my.ini" For Output As #1
Print #1, "1"
Print #1, "2"
Print #1, "3"
Close #1

Is there a way to modify/write values to specific lines only, for
example modifying the "2" value on line 2 to "5", without overwriting
the other lines with their unchanged values?

Thanks for any feedback!

Jim
 
P

Peter T

Although an ini file is just a plain text file ini files have a structure

[SectionName1]
Keyname1=abc
Keyname2=123

If your ini file is like this all you need to know is the section-name(s)
and key-name(s) you are concerned with (and of course the file-name & path).
APIs GetPrivateProfileString and WritePrivateProfileString to read/write the
values will do all the work for you. I'm sure if you search these terms
together with something like "modify ini file" you will find hundreds of
examples.

Regards,
Peter T
 
C

Chip Pearson

Jim,

The following code shows two ways you can accomplish this. The methods
are both commented out. Uncomment the block you want to use.

The first block replaces the content based on only the line number. In
the code, line 2 will be entirely replaced by the content of the
NewLineText. This does not examine the content of the file in any way.
It simply replaces a specific line in the file based on the line
number.

The second block examines the content of each line and if some
criteria (that you define) matches the line, that line is replaced by
the content of NewLineText. The line number is ignored -- only the
content is examined.

In code, leave intact as show all the code between the
' common code
and
' end common code
comment markers.

Uncomment the block of code between the lines marked with '<<<<.
Choose the first block for line number handling or uncomment the
second block of code for content processing.

Sub AAA()
Dim S As String
Dim FName As Variant
Dim FNum As Integer
Dim Full As String
Dim LineNum As Long
Dim NewLineText As String

' common code
FName = "D:\Settings.txt"
FNum = FreeFile
NewLineText = "1234"
Open FName For Input Access Read As #FNum
' end common code

''>>>> replace by line number
'Do Until EOF(FNum)
' Line Input #FNum, S
' LineNum = LineNum + 1
' If LineNum <> 2 Then
' Full = Full & S & vbCrLf
' Else
' Full = Full & NewLine & vbCrLf
' End If
'Loop
'Close #FNum
''>>>> end replace by line number


''>>>> replace by content
'Do Until EOF(FNum)
' Line Input #FNum, S
' ' your test conditions, e.g.,
' If Left(S, 2) = "cc" Then
' Full = Full & NewLineText & vbCrLf
' Else
' Full = Full & S & vbCrLf
' End If
'Loop
'Close #FNum
''>>>> end replace by content

' common code
FNum = FreeFile
Open FName For Output Access Write As #FNum
Print #FNum, Full
Close #FNum
' end common code
End Sub

If you are creating the ini file for a new application, I would
recommend that you go down the XML route rather than the INI road. For
just a bit more effort, you can have a much more functional
configuration file.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top