Task

Replace every occurring instance of a piece of text in a group of text files with another one.



For this task we want to replace the text   "Goodbye London!"   with   "Hello New York!"   for a list of files.




 
nomainwin
 
file$( 1) ="data1.txt"
file$( 2) ="data2.txt"
file$( 3) ="data3.txt"
 
 
for i =1 to 3
    open file$( i) for input as #i
        orig$ =input$( #i, lof( #i))
    close #i
 
    dummy$ =FindReplace$( orig$, "Goodbye London!", "Hello New York!", 1)
 
    open "RC" +file$( i) for output as #o
        #o dummy$;
    close #o
next i
 
end
 
function FindReplace$( FindReplace$, find$, replace$, replaceAll)
'   Target string, string to find, string to replace it with, flag 0/1 for 'replace all occurrences'.
    if ( ( FindReplace$ <>"") and ( find$ <>"") ) then
        fLen =len( find$)
        rLen =len( replace$)
        do
            fPos =instr( FindReplace$, find$, fPos)
            if not( fPos) then exit function
            pre$            =left$( FindReplace$, fPos -1)
            post$           =mid$( FindReplace$, fPos +fLen)
            FindReplace$    =pre$ +replace$ +post$
            fPos            =fPos +( rLen -fLen) +1
        loop while (replaceAll)
    end if
end function
 