Skip to content
⌘ NSIS Forum Archive

Replace string in text file at compile time

11 posts

qwertymodo#

Replace string in text file at compile time

I need to replace a string inside of a text file at compile time, in the same way that the ReplaceInFile header does. I know that I COULD write a separate script that does it via the command-line, then call


!system '"${NSISDIR}\makensis.exe" "textreplacescript.nsi"' = 0
!system '"textreplace.exe" "commandlineargs"' = 0
!delfile "textreplace.exe"
Is there an easier way?
jpderuiter#


Sorry, I was too quick...
This will not work at compile time.
qwertymodo#
So, the whole point of this question was that I have portable-ized a few programs for the U3 platform, and anyone who has ever perused PortableApps.com knows how useful NSIS is for creating portable application launchers. Unfortunately, I did have to go the route I had indicated, but it ended up not being so bad. I actually added a few more functions in this manner as well. My one gripe was having to use separate helper scripts, which just made the source folder all ugly.

Then I discovered the wonders of !system 'echo ... >> mytempfile'

Well, one thing led to another, and it was just too cool using runtime commands at compile time, and then I was like "hey I'll bet I could take all of these helper scripts I'm writing and merge them all into one!"

Yes, I did actually managed to write a very useful script whose output file does NOTHING.

...I get the feeling that no one else has done this before...

...perhaps with good reason...




...I scare myself sometimes...
{_trueparuex^}#
Try this.

!define /file BUNCHASTUFF somefile.txt

!searchreplace NEWSTUFF "${BUNCHASTUFF}" "foo" "bar"
...

!delfile somefile.txt
!appendfile somefile.txt "${NEWSTUFF}"
Also you could use the NSIS !echo command instead of using !system command to call echo in your script.

PaR
{_trueparuex^}#
I just realized that code probably won't work with script files or with any other files that breaks the parameter quoting. 🙁

PaR
qwertymodo#
Hmm... I'll have to try that. Can !echo be used with >> the same as the system echo can? I mean, what is the actual difference? And as for breaking parameter quoting, I can usually get around that with $\"
kichik#
There's no !echo, only !appendfile and it works on all platforms and doesn't depend on the existence of echo or platform dependent command line parsing.
qwertymodo#
Well, there is an !echo command, I guess it just outputs only to the compiler, but thanks for pointing me at !appendfile, it's exactly what I needed.

...also, I'm not sure what you mean by platform-independent, as I'm pretty sure NSIS is Windows only anyways...
Afrow UK#
NSIS can build on other platforms other than Windows (but only build installers that run on Windows).

Stu
qwertymodo#
Oh, right, I knew that... just for some reason I wasn't thinking about the fact that it's a compile-time command, which would depend on the compiling system :P Anyway, problem solved, so thanks for the input 🙂