Using Replace to replace data in a file does not work (Solved)

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Using Replace to replace data in a file does not work (Solved)

Post by mrcoollion » Wed Mar 08, 2023 7:51 pm

Hello LC friends,

I thought I could replace data in a file directly with the replace command. But nothing happens. I also get no error information.
Any ideas? Below is the code snippet I made.

Variable tFileURL holds the Path and filename e.g. C:/foldername/filename.html
variable tGraphLabel holds the name of the graphic that should replace ***GraphicLabel*** in the file

Code: Select all

   put quote&" file: "&tFileURL&quote into tURLInfo
   try
      replace "***GraphicLabel***" with tGraphLabel in URL tURLInfo
   catch tError
   end try
Regards,

Paul
Last edited by mrcoollion on Wed Mar 08, 2023 10:56 pm, edited 1 time in total.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Using Replace to replace data in a file does not work

Post by Klaus » Wed Mar 08, 2023 9:35 pm

Hi Paul,

you end with this in tUrlInfo -> " file: tFileURL" and LC obviuosly does not like this. :-)
Do this:

Code: Select all

...
try
   ## put quote&" file: "&tFileURL&quote into tURLInfo
   replace "***GraphicLabel***" with tGraphLabel in URL ("file:" & tFileURL)
   catch tError
end try
...
Best

Klaus

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Using Replace to replace data in a file does not work

Post by SparkOut » Wed Mar 08, 2023 9:37 pm

This needs to be done by reading the file in, making the changes, and writing it back out. (I think...)
Or by specifying the location of your placeholder text and putting the replacement into that location of the file, eg

Code: Select all

put "new text" into char 1 to 17 of line 55 of url ("file:" & tFileURL)
Or depending on your circumstances and need, you might the power of *merge* quite useful. If you have a template file with placeholders marked with square brackets

Code: Select all

standard text [[tGraphLabel]] more text
then the expression in the merge placeholder will be merged (ie the tGraphLabel variable will be evaluated and its value replaced in the merged result. You can then write it out to a new file.

matthiasr
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 190
Joined: Sat Apr 08, 2006 7:55 am
Location: Lübbecke, Germany
Contact:

Re: Using Replace to replace data in a file does not work

Post by matthiasr » Wed Mar 08, 2023 10:01 pm

mrcoollion wrote:
Wed Mar 08, 2023 7:51 pm

Variable tFileURL holds the Path and filename e.g. C:/foldername/filename.html
variable tGraphLabel holds the name of the graphic that should replace ***GraphicLabel*** in the file

Code: Select all

   put quote&" file: "&tFileURL&quote into tURLInfo
   try
      replace "***GraphicLabel***" with tGraphLabel in URL tURLInfo
   catch tError
   end try
Regards,

Paul
You have to change some parts of your script to get it working

Code: Select all

   put "file:"&tFileURL into tURLInfo
   try
      replace "***GraphicLabel***" with tGraphLabel in URL tURLInfo
   catch tError
   end try
Although that works, i would use value("tURLInfo") to reference to the content of tURLInfo.

Code: Select all

  replace "***GraphicLabel***" with tGraphLabel in URL value("tURLInfo")

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Using Replace to replace data in a file does not work

Post by mrcoollion » Wed Mar 08, 2023 10:55 pm

Thank you all for helping me.

It now works.
What a difference a small code change and some LC forum friends can make :D .

Regards,

Paul

Post Reply

Return to “Talking LiveCode”