viewpoint-particle

Author Topic: Search and replace one line in file  (Read 119 times)

Jeremy Whilde

  • Jr. Member
  • **
  • Posts: 52
Search and replace one line in file
« on: May 22, 2019, 08:11:14 am »
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

  • Tech Support
  • *****
  • Posts: 3016
    • WinBatch
Re: Search and replace one line in file
« Reply #1 on: May 22, 2019, 08:41:16 am »
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

  • Jr. Member
  • **
  • Posts: 52
Re: Search and replace one line in file
« Reply #2 on: May 22, 2019, 09:35:42 am »
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

  • Tech Support
  • *****
  • Posts: 3016
    • WinBatch
Re: Search and replace one line in file
« Reply #3 on: May 22, 2019, 01:15:43 pm »
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