Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1103
  • Last Modified:

Renaming INI section names

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
vulpine
Asked:
vulpine
  • 4
  • 3
1 Solution
 
DabasCommented:
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
 
DabasCommented:
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
 
vulpineAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
vulpineAuthor Commented:
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
 
vulpineAuthor Commented:
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
 
vulpineAuthor Commented:
Got me going in the direction but not a complete answer.
0
 
DabasCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now