Page 2 of 2 <12
Topic Options
#111494 - 2004-01-09 05:14 AM Re: Need Script to replace strings in a text file
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Wow! This is starting to look a lot better than the simple HOSTS change script:
Update/change HOSTS file

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#111495 - 2004-01-09 03:37 PM Re: Need Script to replace strings in a text file
@lejo @rias Offline
Starting to like KiXtart

Registered: 2003-09-02
Posts: 100
Loc: Medellin, Colombia
Try This...
;to read file and put in array
Function ReadFile($file)
Dim $lf, $t
$lf=Chr(10)
$f=FreeFileHandle
$=Open($f,$file)
If @error Exit @error EndIf
Do $t=$t+$lf+ReadLine($f) Until @error
$=Close($f)
$ReadFile=Split(SubStr($t,2),$lf)
EndFunction

;func to replace string a to b
Function ReplaceSTR($string, $from, $to)
If InStr($string, $from)
$string=$to
$cont=$cont+1
Exit 0
Else
$ReplaceSTR=$string
Exit 1
EndIf
EndFunction

;func to write array in a new file
Function WriteFile($array,$file)
Dim $f
$f=FreeFileHandle
$=Open($f,$file,5)
If @error Exit @error EndIf
For Each $line In $array
$=WriteLine($f,$line + @crlf)
Next
$=Close($f)
Exit @error
EndFunction

;cod for replace string in the file using funcs
$Usuario_Anterior="xxxx"
$Usuario_Actual="yyyy"

$file='%allusersprofile%\Datos de programa\IBM\Personal Communications\Medellin.acg'
$arrFile=ReadFile($file)
For $line =0 to Ubound($arrFile)
$arrFile[$line]=ReplaceSTR($arrFile[$line],$Usuario_Anterior,$Usuario_Actual)
Next
$=WriteFile($arrFile,$file)

_________________________
Look always 4 the best...

Top
#111496 - 2004-01-09 03:40 PM Re: Need Script to replace strings in a text file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
ReplaceSTR() is just a convoluted way of SplitJoining a string as has already been explained in numerous posts.
Code:

$string=join(split($string,$search),$replace)

_________________________
There are two types of vessels, submarines and targets.

Top
Page 2 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 565 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.05 seconds in which 0.024 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