Skip to content
⌘ NSIS Forum Archive

loop issue

3 posts

MyPC8MyBrain#

loop issue

Hi everyone 🙂
im having a small loop issue i need little help with,
i am trying to search in file first to find a specific entry; followed by another search and replace,

my start test.txt file looks like this
mod={1234}
size=11
mod={1235}
size=11 

i am trying to search for "mod={1234}" than replace the "size=*" that follows,
as a starting point the number after "size=" can be any number,
i am searching for the next line start and replacing the entire line after the first match is found,
it seem to work accept its looping on me; it keeps finding "size=" and replacing it throughout the file,

i am trying to get to this
mod={1234}
size=22
mod={1235}
size=11 
but i keep ending up with this
mod={1234}
size=22
mod={1235}
size=22 
why is it looping instead of replacing just the first match?
this is the code i am currently using,

!include "TextFunc.nsh"
!insertmacro LineFind
!define FIND1 "mod={1234}"
!define FINDNEXT "size="
!define REPL1 "size=22"
outFile "test.exe"
Function LineFindCallback
        StrLen $3 "${FIND1}"
        StrLen $0 "${FINDNEXT}"
        StrCpy $1 "$R9" $0
        StrCmp $1 "${FINDNEXT}" 0 End
        StrCpy $R9 "${REPL1}$\r$\n"
        End:
        Push $0
FunctionEnd
Section Install
        ${LineFind} "test.txt" "test.txt" "1:-1" "LineFindCallback"
        IfErrors 0 +2
        MessageBox MB_OK "Error"
SectionEnd 

TIA
Chris
Anders#
You are doing StrLen $3 "${FIND1}" and then ignore the result of it?

It seems to me like you should look for the "mod" line and when you find it you store that state somewhere so you know which "size" line you should modify.
MyPC8MyBrain#
Thank you Anders <3
indeed i am ignoring $3 as its the encore only,
i need to find and replace the line that comes right after $3,
as $0 string will show up many times; always below a distinct but similar entry to $3 format,
i first find my "encore" line with $3; from there i find again $0 and replace,

the problem is that it keeps looping finding $0 and replacing it multiple times instead of a single time edit followed by exiting,

i need to find string 1, immediately followed by the first instance found of string 2 following string 1 and replace it,

am i going at it the wrong way?

TIA
Chris