Globally replace text in several files: Difference between revisions

Content added Content deleted
(Initial FutureBasic task solution added)
(Improved version. Adheres more closely to intention of task.)
Line 648: Line 648:
url = fn URLByAppendingPathComponent( desktopURL, fileNames[i] )
url = fn URLByAppendingPathComponent( desktopURL, fileNames[i] )
url = fn URLByAppendingPathExtension( url, @"txt" )
url = fn URLByAppendingPathExtension( url, @"txt" )
CFStringRef fullText = fn StringWithFormat( @"%@ What an interesting city.", originalText )
fn StringWriteToURL( originalText, url, YES, NSUTF8StringEncoding, NULL )
fn StringWriteToURL( fullText, url, YES, NSUTF8StringEncoding, NULL )
MutableArrayAddObject( mutURL, url )
MutableArrayAddObject( mutURL, url )
next
next
Line 656: Line 657:
fileContentStr = fn StringWithContentsOfURL( mutURL[i], NSUTF8StringEncoding, NULL )
fileContentStr = fn StringWithContentsOfURL( mutURL[i], NSUTF8StringEncoding, NULL )
NSLog( @"Contents at: %@ = %@", fn URLPath( mutURL[i] ), fileContentStr )
NSLog( @"Contents at: %@ = %@", fn URLPath( mutURL[i] ), fileContentStr )
CFStringRef modifiedText = fn StringByReplacingOccurrencesOfString( fileContentStr, originalText, replacementText )
fn StringWriteToURL( replacementText, mutURL[i], YES, NSUTF8StringEncoding, NULL )
fn StringWriteToURL( modifiedText, mutURL[i], YES, NSUTF8StringEncoding, NULL )
next
next
NSLog( @"\nReplacement text:" )
NSLog( @"\nReplacement text:" )
Line 672: Line 674:
<pre>
<pre>
Original text:
Original text:
Contents at: /Users/ken/Desktop/file1.txt = Goodbye London!
Contents at: /Users/ken/Desktop/file1.txt = Goodbye London! What an interesting city.
Contents at: /Users/ken/Desktop/file2.txt = Goodbye London!
Contents at: /Users/ken/Desktop/file2.txt = Goodbye London! What an interesting city.
Contents at: /Users/ken/Desktop/file3.txt = Goodbye London!
Contents at: /Users/ken/Desktop/file3.txt = Goodbye London! What an interesting city.


Replacement text:
Replacement text:
Contents at: /Users/ken/Desktop/file1.txt = Hello New York!
Contents at: /Users/ken/Desktop/file1.txt = Hello New York! What an interesting city.
Contents at: /Users/ken/Desktop/file2.txt = Hello New York!
Contents at: /Users/ken/Desktop/file2.txt = Hello New York! What an interesting city.
Contents at: /Users/ken/Desktop/file3.txt = Hello New York!
Contents at: /Users/ken/Desktop/file3.txt = Hello New York! What an interesting city.
</pre>
</pre>





=={{header|Go}}==
=={{header|Go}}==