Search and replace one line in file

Started by Jeremy Whilde, May 22, 2019, 08:11:14 AM

Previous topic - Next topic

Jeremy Whilde

We need to find one line in an application ini file and replace this, I have managed to almost achieve this but in some instances, the target line has a value as well as a target name so we look for targetData= and need to replace this with "targetData=specifc_value". The following code works except if there is an existing targetData value you get "targetData=specific_value+existing_value"

infind="targetData="
outReplace="targetData=1a2b3c4d5e6f"

fnamein="OurApp.ini"
fnameout="OurAppOut.ini"

if fileexist(fnamein)==0
   message("Warning","File %fnamein% not found")
   exit
endif

insize=FileSize(fnamein)
inbuffer= BinaryAlloc(insize+100)
BinaryRead(inbuffer, fnamein)
BinaryReplace(inbuffer, infind, outReplace, @FALSE)
BinaryWrite(inbuffer,fnameout)
BinaryFree(inbuffer)

is there a way to do this using this code or do I need to look at another method?

Thanks JW

td

Why not just use the IniWritePvt function?  It might be as simple as a one-line script and is certainly much simpler than using Binary functions.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Jeremy Whilde

Thanks, Tony OK yes this seems the most logical and simple way as this is an ini file I had forgotten about the private ini functions as this file had not until recently been an ini file for the application concerned just a text file with settings.

Just tried it and of course, it works, IniWritePvt is the perfect solution for changing ini settings thanks again.

td

What was I about to say?  Oh yeah.  I am the last person to offer up criticism for forgetting something... 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade