Page 1 of 2 12>
Topic Options
#145120 - 2005-08-09 08:03 PM Search for and Replace Text in INI
TidusBlue Offline
Fresh Scripter

Registered: 2005-08-08
Posts: 5
I need to add a section in our script that will search an .ini file and find all instances of a certain text and replace that with different text.

We've currently changed the name of one of our servers and I'm looking for a way to automatically change the contents of an .ini file to point to the new server name. Ex:

embosser=\\abcnt04\printer123
needs changed to
embosser=\\abcnt86\printer123

I've looked at the readprofilestring and write profile string, but it doesn't seem to be what I'm looking for, or maybe I don't know know the right way to do it using those. Thanks for you help.

Top
#145121 - 2005-08-09 08:17 PM Re: Search for and Replace Text in INI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
they do fine.
read all the values with and...
well, try this:
Code:

$oldName = "mybadserver"
$newName = "myfreshserver"
for each $section in readprofilestring("my\file","","")
for each $value in readprofilestring("my\file",$section,"")
$splittedValue=split(readprofilestring("my\file",$section,$value),$oldName)
if 0<ubound($splittedValue)
$nul = writeprofilestring("my\file",$section,join($splittedValue,$newName))
endif
next
next



vóila
_________________________
!

download KiXnet

Top
#145122 - 2005-08-09 08:17 PM Re: Search for and Replace Text in INI
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
You can use something like this...

Code:
;**** Last Modified on 20-08-2003 at 11:52:44 AM by Co****


Break on
If @producttype = 'Windows XP Professional'

Dim $printers, $printer, $rc

$printers=arrayenumkey('HKCU\Printers\Connections')

For Each $printer In $printers
If InStr($printer,'prtsrv01')
$printer=Split($printer,',')
$old='\\'+$printer[2]+'\'+$printer[3]
$new="\\prtsrv02\" + $printer[3]
? '-I- Connection found: '+$old
$rc=DelPrinterConnection($old)
If @error
? '-E- Error deleting connection to '+$old+' (EC='+@error+')'
Else
? '-I- Succesfully deleted connection to '+$old
EndIf

$rc=AddPrinterConnection($new)
If @ERROR
? '-E- Error connecting to '+$new+' (EC='+@error+')'
Else
? '-I- Succesfully added connection to '+$new
EndIf
EndIf

;Exit

;FUNCTION
;
;NAME ArrayEnumKey
;
;ACTION Creates an array of names of the subkeys contained in a registry key or subkey
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.2 (added error codes)
; 1.1
;
;DATE CREATED 2001/12/05
;
;DATE MODIFIED 2003/05/17
;
;KIXTART 4.12+
;
;SYNTAX ARRAYENUMKEY($subkey)
;
;PARAMETERS SUBKEY
; Required string containing the key or subkey for which the subkeys will be enumerated
;
;RETURNS Array containing the subkeys
;
;REMARKS none
;
;DEPENDENCIES none
;
;EXAMPLE $retcode=arrayenumkey('HKEY_USERS')
;
;KIXTART BBS http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=12&t=000064
;
Function arrayenumkey($regsubkey)
Dim $retcode, $subkeycounter, $currentsubkey, $subkeyarray

If NOT KeyExist($regsubkey)
Exit 87
EndIf

$subkeycounter=0
Do
$currentsubkey=EnumKey($regsubkey,$subkeycounter)
If NOT @ERROR
ReDim preserve $subkeyarray[$subkeycounter]
$subkeyarray[$subkeycounter]=$currentsubkey
$subkeycounter=$subkeycounter+1
EndIf
Until @ERROR

$arrayenumkey=$subkeyarray
Exit 0
EndFunction

_________________________
Co


Top
#145123 - 2005-08-09 08:18 PM Re: Search for and Replace Text in INI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hehee!
I wrote whole custom code before you got to paste yours!
I got my speed back! yehaa!
_________________________
!

download KiXnet

Top
#145124 - 2005-08-09 08:22 PM Re: Search for and Replace Text in INI
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
OK, Lonk you win...
_________________________
Co


Top
#145125 - 2005-08-09 09:05 PM Re: Search for and Replace Text in INI
TidusBlue Offline
Fresh Scripter

Registered: 2005-08-08
Posts: 5
Ok, so I've put tested your code with Kixtarter, but it doesn't seem to be working.
Code:

$oldName = "abcnt04"
$newName = "abcnt32"
for each $section in readprofilestring("C:\blah1.ini","","")
for each $value in readprofilestring("C:\blah1.ini",$section,"")
$splittedValue=split(readprofilestring("C:\oasis1.ini",$section,$value),$oldName)
if 0<ubound($splittedValue)
$nul = writeprofilestring("C:\blah1.ini",$section,join($splittedValue,$newName))
endif



Edited by TidusBlue (2005-08-09 09:07 PM)

Top
#145126 - 2005-08-09 09:08 PM Re: Search for and Replace Text in INI
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
is that all of the code? if not, you are missing a few lines.
Top
#145127 - 2005-08-09 09:11 PM Re: Search for and Replace Text in INI
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Quote:

they do fine.
read all the values with and...
well, try this:
Code:

$oldName = "mybadserver"
$newName = "myfreshserver"
for each $section in readprofilestring("my\file","","")
for each $value in readprofilestring("my\file",$section,"")
$splittedValue=split(readprofilestring("my\file",$section,$value),$oldName)
if 0<ubound($splittedValue)
$nul = writeprofilestring("my\file",$section,join($splittedValue,$newName))
endif
next
next



vóila




err... les,


readprofilesstring() does not return an array value...

this dont work
Code:

for each $section in readprofilestring("my\file","","")

next


Top
#145128 - 2005-08-09 09:17 PM Re: Search for and Replace Text in INI
TidusBlue Offline
Fresh Scripter

Registered: 2005-08-08
Posts: 5
I should also let you guys know that I'm not very versed with scripting... we're just tired of having other people always changing our scripts, so we're looking into it ourselves...
in other words, I'm pretty new to this.

Top
#145129 - 2005-08-09 09:21 PM Re: Search for and Replace Text in INI
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
MMM well, mine works... Ok,Ok, it Depends..It doesn't work for Win9x pc's...
_________________________
Co


Top
#145130 - 2005-08-09 09:22 PM Re: Search for and Replace Text in INI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
bryce, dunno where you see les but...

indeed.
tidusblue, my code lagged something, try this:
Code:

$oldName = "mybadserver"
$newName = "myfreshserver"
for each $section in split(left(readprofilestring("my\file","",""),-1),chr(10))
for each $value in split(left(readprofilestring("my\file",$section,""),-1),chr(10))
$splittedValue=split(readprofilestring("my\file",$section,$value),$oldName)
if 0<ubound($splittedValue)
$nul = writeprofilestring("my\file",$section,join($splittedValue,$newName))
endif
next
next

_________________________
!

download KiXnet

Top
#145131 - 2005-08-09 09:24 PM Re: Search for and Replace Text in INI
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
Yeah, was searching for Les too

Edited by Co (2005-08-09 09:25 PM)
_________________________
Co


Top
#145132 - 2005-08-09 09:25 PM Re: Search for and Replace Text in INI
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Did someone mention my name?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#145133 - 2005-08-09 09:26 PM Re: Search for and Replace Text in INI
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Code:
$oldServer = 'server1'
$newServer = 'server2'

$inifile = '.\test.ini'
;get an array of the sections
$sections = split(ReadProfileString($inifile,'',''),CHR(10))
redim preserve $sections[ubound($sections)-1] ;trim the array by one


for each $section in $sections
;get an array of the keys in a section
$keys = split(ReadProfileString($inifile,$section,''),CHR(10))
redim preserve $keys[ubound($keys)-1] ;trim the array by 1

for each $key in $keys
$data = ReadProfileString($inifile,$section,$key)

;if the data of a key has the old server int it, replace it with the new server name
If InSTR($data,$oldserver)
$data = join(split($data,$oldserver),$newserver)

;save data back to the ini file
$ = WriteProfileString($inifile,$section,$key,$data)
EndIf
next
next



Edited by Bryce (2005-08-09 09:27 PM)

Top
#145134 - 2005-08-09 09:28 PM Re: Search for and Replace Text in INI
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
oops!
Top
#145135 - 2005-08-09 09:29 PM Re: Search for and Replace Text in INI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
bryce, your code is way too long
_________________________
!

download KiXnet

Top
#145136 - 2005-08-09 09:31 PM Re: Search for and Replace Text in INI
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
it's all the comments... i can understand your confusion
Top
#145137 - 2005-08-09 09:42 PM Re: Search for and Replace Text in INI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sure, but you also have some instr stuff there, huh?
_________________________
!

download KiXnet

Top
#145138 - 2005-08-09 10:40 PM Re: Search for and Replace Text in INI
TidusBlue Offline
Fresh Scripter

Registered: 2005-08-08
Posts: 5
Bryce -
Thanks, that seems to work for what I need it to do.

Top
#145139 - 2005-08-09 11:28 PM Re: Search for and Replace Text in INI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
tidus, so you missed my new code?
damn, it was so tidy and neat.
_________________________
!

download KiXnet

Top
Page 1 of 2 12>


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

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

Generated in 0.063 seconds in which 0.036 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