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.




      FindThis$ = "Goodbye London!"
      ReplaceWith$ = "Hello New York!"
 
      DIM Files$(3)
      Files$() = "C:\test1.txt", "C:\test2.txt", "C:\test3.txt", "C:\test4.txt"
 
      FOR f% = 0 TO DIM(Files$(),1)
        infile$ = Files$(f%)
        infile% = OPENIN(infile$)
        IF infile%=0 ERROR 100, "Failed to open file " + infile$
        tmpfile$ = @tmp$+"replace.txt"
        tmpfile% = OPENOUT(tmpfile$)
 
        WHILE NOT EOF#infile%
          INPUT #infile%, a$
          IF ASCa$=10 a$ = MID$(a$,2)
          l% = LEN(FindThis$)
          REPEAT
            here% = INSTR(a$, FindThis$)
            IF here% a$ = LEFT$(a$,here%-1) + ReplaceWith$ + MID$(a$,here%+l%)
          UNTIL here% = 0
          PRINT #tmpfile%, a$ : BPUT #tmpfile%,10
        ENDWHILE
        CLOSE #infile%
        CLOSE #tmpfile%
 
        OSCLI "DEL """ + infile$ + """"
        OSCLI "REN """ + tmpfile$ + """ """ + infile$ + """"
      NEXT
      END