Skip to main content
March 28, 2014
Answered

Search and Replace text

  • March 28, 2014
  • 1 reply
  • 1141 views

Apologies for what is no doubt a simple question - I'm trying to help out IT and I'm not a programmer, although maybe one day...

Anyway, I'm going to need to read a text file, and search for a string in that text file and replace it.  I see "replace" as a function but I'm not familiar with how to use it.  Poking around I found <cffile action=append> and used that to add a line to a text document.  (Just a notepad .txt)

Ultimately I'll need to unzip a folder, search and replace the string in a specific file in there, and then re-zip the file, but we'll get to that later.  I managed to unzip a folder already, that's easy enough.  Putting it all together is my current challenge.

I'm using ColdFusion Builder 2.0

Thanks for any help you guys can give.

This topic has been closed for replies.
Correct answer WolfShade

file.txt: Contains several lines that all say "All Work And No Play Makes Jack A Dull Boy".

<cffile action="read" file="C:\file.txt" variable="myFile">

<cfset myFile = Replace(myFile,"Dull","Bored","all")>

<cffile action="write" file="C:\file.txt" output="#myFile#" addnewline="no" fixnewline="no">

<cffile action="read" file="C:\file.txt" variable="myNewFile">

<cfoutput>#myNewFile#</cfoutput>

^_^

1 reply

WolfShade
WolfShadeCorrect answer
Legend
March 28, 2014

file.txt: Contains several lines that all say "All Work And No Play Makes Jack A Dull Boy".

<cffile action="read" file="C:\file.txt" variable="myFile">

<cfset myFile = Replace(myFile,"Dull","Bored","all")>

<cffile action="write" file="C:\file.txt" output="#myFile#" addnewline="no" fixnewline="no">

<cffile action="read" file="C:\file.txt" variable="myNewFile">

<cfoutput>#myNewFile#</cfoutput>

^_^

March 28, 2014

Very groovy.  Thanks!

WolfShade
Legend
March 28, 2014

You're welcome.  Thanks for marking my response as correct.  Have a great weekend.