?
Solved

Renaming INI section names

Posted on 2003-03-08
7
Medium Priority
?
1,010 Views
Last Modified: 2012-05-04
I need to edit .ini files extensivly in my application
I can add and remove keys values and sections, but i dont know how to rename sections?

Can someone help with renaming in ini files??

Cheers
0
Comment
Question by:vulpine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 8096624
I would suggest you use a TextStream (as part of the FileSystemObject) to search the .ini file line by line and replace the section name with its replacement.

Dabas
0
 
LVL 27

Accepted Solution

by:
Dabas earned 300 total points
ID: 8096636
Actually, it is not hard at all, and simpler than I suggested in my previous post.

In Project -> References, tick next to Microsoft Scripting Runtime

This code will do what you are asking for:
I am asuming the full path and name of the .ini file is held in Inifilename.
strCurrentSection is the name of the section now
strNewSection is the name you want to rename it to.

    Dim ts As TextStream
    Dim fso As New FileSystemObject
    Dim strIniFileText as string

    Set ts = fso.OpenTextFile(Inifilename)
    strIniFileText = ts.ReadAll
    Replace strIniFileText, "[" & strCurrentSection & "]", "[" & strNewSection & "]"
0
 

Author Comment

by:vulpine
ID: 8096723
Hmm I was thinking of the INI API, but your suggestion seems to solve the problem.

I need to write the changes back to the text file u left that out, but thanks.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:vulpine
ID: 8096752
From your sugestion this is what i have come up with, but it doesnt work. The changes dont seem to be made.


Function RenameSection(CurrentName As String, NewName As String)
   
   Dim ts As TextStream
   Dim fso As New FileSystemObject
   Dim strIniFileText As String
   Dim strINIFile As String
   
   strINIFile = "C:\inifilesname.ini"

   Set ts = fso.OpenTextFile(strINIFile)
   strIniFileText = ts.ReadAll
   Replace strIniFileText, "[" & CurrentName & "]", "[" & NewName & "]"
   
    Set ts = fso.CreateTextFile(strINIFile, True)
    ts.WriteLine strIniFileText
   
    Set ts = Nothing
    Set fso = Nothing
       
End Function
0
 

Author Comment

by:vulpine
ID: 8096793
DUUHH, I realised you need to reassign the new string to strIniFileText. Thanks for the pointer. For those interested new code:



Function RenameSection(CurrentName As String, NewName As String)
   
  Dim ts As TextStream
  Dim fso As New FileSystemObject
  Dim strIniFileText As String
  Dim strINIFile As String
  
  strINIFile = "C:\inifilesname.ini"
  Set ts = fso.OpenTextFile(strINIFile)
  strIniFileText = ts.ReadAll
  strIniFileText = Replace strIniFileText, "[" & CurrentName & "]", "[" & NewName & "]"
   
   Set ts = fso.CreateTextFile(strINIFile, True)
   ts.WriteLine strIniFileText
   
   Set ts = Nothing
   Set fso = Nothing
       
End Function
0
 

Author Comment

by:vulpine
ID: 8096794
Got me going in the direction but not a complete answer.
0
 
LVL 27

Expert Comment

by:Dabas
ID: 8097195
Yep, you are right, sorry for that.
I was coding from memory, and assumed the replace would actually change the text.
Glad I could help!

Dabas

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

765 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