Page 1 of 1 1
Topic Options
#122001 - 2004-06-30 11:44 AM Search and replace in an .ini file
Rbusch Offline
Lurker

Registered: 2004-06-30
Posts: 2
Loc: Oslo, Norway
Hi...
Is it possible to create a function that can do search and replace in an .ini file. I need to change a server name under as much as 300 different sections in an .ini file, and I like to do this as a function in a login script. The .ini file looks like this:

[App1]
InstallServer=Server1

[App2]
Installserver=Server1

[App3]
Installserver=Server1

I need to change Server1 to Server2. I can use writeprofilestring, but then i need to know all the sections (It's over 300 of them). Do anyone now how to make function to search and replace the servername in this .ini file.

I need som help here...

RB

Top
#122002 - 2004-06-30 01:03 PM Re: Search and replace in an .ini file
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
If you leave the entry name in ReadProfileString() blank you will get all the entries in a section. If you leave the section name blank you will get all the sections in a file.

Use this technique to enumerate all the sections and entries.

Here is a simple (untested!) example:
Code:
$sMyIni=".\serverlist.ini"
$sOldString="Server1"
$sNewString="Server2"

For Each $sSection in Split(ReadProfileString($sMyIni,"",""),Chr(10))
If $sSection
"Checking section "+$sSection+@CRLF
For Each $sEntry in Split(ReadProfileString($sMyIni,$sSection,""),Chr(10))
If $sEntry
$sValue=ReadProfileString($sMyIni,$sSection,$sEntry)
"Entry "+$sEntry+" has value "+$sValue+" status:"
If $sValue=$sOldString
" **UPDATED**"+@CRLF
$=WriteProfileString($sMyIni,$sSection,$sEntry,$sNewString)
Else
" No change"+@CRLF
EndIf
EndIf
Next
EndIf
Next


Top
#122003 - 2004-06-30 02:09 PM Re: Search and replace in an .ini file
Rbusch Offline
Lurker

Registered: 2004-06-30
Posts: 2
Loc: Oslo, Norway
Man... It worked great!!!!! thnx!!!
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 661 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.052 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org