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]