Page 1 of 1 1
Topic Options
#73238 - 2003-02-19 09:10 AM need script
barjoe Offline
Lurker

Registered: 2003-02-19
Posts: 2
Has someone a script who search a word in a file and replace it by an other

Please help me, thanks

Top
#73239 - 2003-02-19 09:12 AM Re: need script
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Joe ,

welcome to the board !

Is it a plain text file or maybe a word doc ?

J.
_________________________



Top
#73240 - 2003-02-19 09:46 AM Re: need script
barjoe Offline
Lurker

Registered: 2003-02-19
Posts: 2
it is the hosts file
Top
#73241 - 2003-02-19 09:50 AM Re: need script
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
from the scratch :



break on

dim $x
$file = @scriptdir + "\test.txt"
$search = "phrase to replace"
$replace = "phrase to insert"
$FileStr = ""

$ = open(1,$file,2)
if not @error
    $x = readline(1)
    while not @error
        if instr($x,$search)
            $FileStr = $FileStr + join(split($x,$search),$replace) + @crlf
        else
            $FileStr = $FileStr + $x + @crlf
        endif
        $x = readline(1)
    loop
    $ = close(1)
    del $file
    $ = open(1,$file,5)
    $ = writeline(1,$FileStr)
    $ = close(1)
    ? "File has been adapted" ?
else
    ? "file open failed : " + @serror ?
endif

"Hit any key to quit ..."

get $



This replaces (if present) $Search by $replace

Possible problem : it adds an additional @crlf at the end
(Ok ... tested with my host file and it adds the additional crlf only if there is none in the original, speak, it adds only once !
Hehe : changed by this all entries called daimlerchrysler.com to chryslerdaimler.com and back [Big Grin]
This scriplet has evil powers if used in a loop to modify remote files [Eek!] )

hth
Jochen

[ 19. February 2003, 11:35: Message edited by: jpols ]
_________________________



Top
#73242 - 2003-02-20 05:57 PM Re: need script
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Please take a look at the UDF Forum, e.g. IsStringInFile - Checks whether a string is present in a file
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.051 seconds in which 0.023 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