#145120 - 2005-08-09 08:03 PM
Search for and Replace Text in INI
|
TidusBlue
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
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
|
|
|
|
#145125 - 2005-08-09 09:05 PM
Re: Search for and Replace Text in INI
|
TidusBlue
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
|
|
|
|
#145127 - 2005-08-09 09:11 PM
Re: Search for and Replace Text in INI
|
Bryce
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
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
|
|
|
|
#145130 - 2005-08-09 09:22 PM
Re: Search for and Replace Text in INI
|
Lonkero
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
|
|
|
|
#145133 - 2005-08-09 09:26 PM
Re: Search for and Replace Text in INI
|
Bryce
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
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
oops!
|
|
Top
|
|
|
|
#145136 - 2005-08-09 09:31 PM
Re: Search for and Replace Text in INI
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
it's all the comments... i can understand your confusion
|
|
Top
|
|
|
|
#145138 - 2005-08-09 10:40 PM
Re: Search for and Replace Text in INI
|
TidusBlue
Fresh Scripter
Registered: 2005-08-08
Posts: 5
|
Bryce - Thanks, that seems to work for what I need it to do.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1471 anonymous users online.
|
|
|