Skip to content
⌘ NSIS Forum Archive

Replace in file

8 posts

fabiochelly#

Replace in file

Hi,

Currently, I use an external nsh file to replace text in files.

Is there a way to replace text in file with the integrated include nsh files: wordfunc, strfunc or else?

Something like:

${StrRepFile} "$FILEPATH" "oldtext" "newtext"
Instructor#
Name "Output"
OutFile "Output.exe"

!include "TextFunc.nsh"
!insertmacro LineFind

!include "WordFunc.nsh"
!insertmacro WordReplace

Section
${LineFind} "C:\ReplaceInFile.txt" "C:\ResultFile.txt" "1:-1" "LineFindCallback"

IfErrors 0 +2
MessageBox MB_OK "Error"
SectionEnd

Function LineFindCallback
${WordReplace} "$R9" "Replace this string" "Replace with this string" "+" $R9

Push $0
FunctionEnd
fabiochelly#
Thank you.

Is it normal to push $0 instead of $R9 at the end of the callback function?

And do you know if there is difference between StrRep and WordReplace?
Instructor#
Push $var      ; If $var="StopLineFind"  Then exit from function
; If $var="SkipWrite" Then skip current line (ignored if "/NUL")
fabiochelly#
What happen if I don't Push something at the end of the callback function? Or if I push an empty string?
Instructor#
If you don't use "StopLineFind", "SkipWrite", you can Push anything else "Push 0", "Push ''" ... You need to Push something, otherwise it will bring to stack corruption.
fabiochelly#
Thank you very much. It works fine.

By the way is there a difference between StrRep (from StrFunc.nsh) and WordReplace?