Page 1 of 1

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

Posted: Wed Mar 08, 2023 7:51 pm
by mrcoollion
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

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

Posted: Wed Mar 08, 2023 9:35 pm
by Klaus
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

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

Posted: Wed Mar 08, 2023 9:37 pm
by SparkOut
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.

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

Posted: Wed Mar 08, 2023 10:01 pm
by matthiasr
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")

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

Posted: Wed Mar 08, 2023 10:55 pm
by mrcoollion
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