How to resolve the algorithm Globally replace text in several files step by step in the FutureBasic 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 FutureBasic 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 FutureBasic programming language

Source code in the futurebasic programming language

include "NSLog.incl"

void local fn GloballyCreateAndReplaceFileText
  NSUInteger                i
  CFURLRef                url
  CFMutableArrayRef    mutURL = fn MutableArrayNew
  CFArrayRef        fileNames = @[@"file1", @"file2", @"file3"]
  CFStringRef  fileContentStr
  CFStringRef    originalText = @"Goodbye London!"
  CFStringRef replacementText = @"Hello New York!"
  
  for i = 0 to len(fileNames) - 1
    CFURLRef desktopURL = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask )
    url = fn URLByAppendingPathComponent( desktopURL, fileNames[i] )
    url = fn URLByAppendingPathExtension( url, @"txt" )
    CFStringRef fullText = fn StringWithFormat( @"%@ What an interesting city.", originalText )
    fn StringWriteToURL( fullText, url, YES, NSUTF8StringEncoding, NULL )
    MutableArrayAddObject( mutURL, url )
  next
  
  NSLog( @"Original text:" )
  for i = 0 to len(mutURL) - 1
    fileContentStr = fn StringWithContentsOfURL( mutURL[i], NSUTF8StringEncoding, NULL )
    NSLog( @"Contents at: %@ = %@", fn URLPath( mutURL[i] ), fileContentStr )
    CFStringRef modifiedText = fn StringByReplacingOccurrencesOfString( fileContentStr, originalText, replacementText )
    fn StringWriteToURL( modifiedText, mutURL[i], YES, NSUTF8StringEncoding, NULL )
  next
  NSLog( @"\nReplacement text:" )
  for i = 0 to len(mutURL) - 1
    fileContentStr = fn StringWithContentsOfURL( mutURL[i], NSUTF8StringEncoding, NULL )
    NSLog( @"Contents at: %@ = %@", fn URLPath( mutURL[i] ), fileContentStr )
  next
end fn

fn GloballyCreateAndReplaceFileText

HandleEvents

  

You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the GW-BASIC programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the BQN programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the AutoIt programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the VBScript programming language