LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7,247
Default How to modify specific lines in a .ini file?

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]



On Sat, 30 Jan 2010 00:49:42 -0800 (PST), Jim
wrote:

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

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create/modify date for specific sheets? Steve Vincent Excel Discussion (Misc queries) 3 September 17th 09 08:25 PM
printing specific lines Jim Office Supply Store Owner Excel Discussion (Misc queries) 1 October 10th 08 11:42 PM
modify a macro to apply to a specific range of cells Dave F Excel Discussion (Misc queries) 2 April 25th 07 03:00 AM
Help...Need to modify data within a column in a .csv file [email protected] Excel Discussion (Misc queries) 11 October 26th 05 11:23 PM
Help...Need to modify data within a column in a .csv file [email protected] Excel Worksheet Functions 11 October 26th 05 11:23 PM


All times are GMT +1. The time now is 02:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"