How to resolve the algorithm Globally replace text in several files step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Globally replace text in several files step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Globally replace text in several files step by step in the FreeBASIC programming language

Source code in the freebasic programming language

Const matchtext = "Goodbye London!"
Const repltext  = "Hello New York!"
Const matchlen  = Len(matchtext)

Dim As Integer x, L0 = 1
dim as string filespec, linein

L0 = 1
While Len(Command(L0))
    filespec = Dir(Command(L0))
    While Len(filespec)
        Open filespec For Binary As 1
        linein = Space(Lof(1))
        Get #1, 1, linein
        Do
            x = Instr(linein, matchtext)
            If x Then
                linein = Left(linein, x - 1) & repltext & Mid(linein, x + matchlen)
            Else
                Exit Do
            End If
        Loop
        Close
        Open filespec For Output As 1
        Print #1, linein;
        Close
        filespec = Dir
    Wend
    L0 += 1
Wend

  

You may also check:How to resolve the algorithm Averages/Median step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the UnixPipes programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the EMal programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the 360 Assembly programming language