Archive: substitute stings in files


substitute stings in files
  I want to make nsis open a file, look for a string, and substitute that for another one of different lenth. I am trying to work with those file commands but I can't make them do what I want, could anyone help me? thanks:)


Have a look at this example:
http://www.clantpa.co.uk/nsis/wiki/index.php/FileRead

Instead of:


StrCmp $2 "line to replace$\r$\n" 0 +2

FileWrite$1 "replacement of line$\r$\n"
>Goto loop
>
use StrReplace and place the result in the new file.

[edit]StrStr -> StrReplace. Sorry, mixed those two up.[/edit]

Invalid command: ReadFile
Error in script "C:\Program Files\NSIS\Test\test1.nsi" on line 72 -- aborting creation process


I tested your example but got this error message? any idea?
line 72: ReadFile $0 $2


Please attach large scripts next time. Script attached here.

ReadFile should be FileRead.


kichik, it worked nicely for me.

It's nice piece of code.


kichik, I changed it to FileRead and it worked. However it not only replaced that line but the whole file.
---original----file.txt---
asdfjskdfja
line to replace
asdfsadf

-------eof------------

---result----file.txt

replaced line

-------eof------------

----what is should be---
asdfjskdfja
replaced line
asdfsadf

-------------------------

any idea what was wrong here?

Also, if you have some spare time;) could you give me an example of how to replace multiple strings.
ex:

find: x123 replace:y123
find: zsdf replace:1232

Thanks for the help!


I can help you
  This is the script I finally use.

I can assure you that it works with NSIS v1.98.

It can replace two strings in a file. As you can see is as simple as adding a nested StrCmp.

Another change is that instead of telling NSIS to jump two lines I added two new Labels. The original script didn't work.

I put a dash in the name of the section to make it a hidden section.


Here's the Section I use:



Section "-Change File"

!define ENCINI "$INSTDIR\file2change.txt"
FileOpen $0 "${ENCINI}" r
GetTempFileName $R0
FileOpen$1 $R0 w
loop:
FileRead $0 $2
IfErrors done
StrCmp$2 "first line to replace$\r$\n" igual dife
igual:
FileWrite $1 "replacement of first line$\r$\n"
Goto loop
dife:
StrCmp $2 "second line to replace$\r$\n" igual2 dife2
igual2:
FileWrite $1 "replacement of second line$\r$\n"
Goto loop
dife2:
FileWrite $1 $2
Goto loop
done:
FileClose $0
FileClose$1
Delete "${ENCINI}"
CopyFiles /SILENT $R0 "${ENCINI}"
>SectionEnd
>

StrCmp $2 "line to replace$\r$\n" 0 +2

should be:

StrCmp $2 "line to replace$\r$\n" 0 +3

I think I fixed that once... =/
I will reupload the script in that thread so it will work if anyone else tries it ;)


Thanks for the help, the code works great. Last question, do you know why the script generates a tmp file in the tmp dir?
ex: nst37.tmp


done:
FileClose $0
FileClose $1
Delete "file.txt"
CopyFiles /SILENT $R0 "$INSTDIR\file.txt"

isn't it suppose to be deleted?


Add Delete $R0 after CopyFiles. I have updated the thread again.


For the benefit of other users, I suggest to add an exemple of this in the nsis package.


(Completely new to nsis, so please bear with me)

I notice that the answers here seem to deal with replacing a whole line of text with another. Is it possible to replace a portion of a line with some specified text? eg read a line from a file, and if it contains the string XXXX replace XXXX by YY?

(Reason: I have an application that needs to create lots of batch files. These files have placeholders for user information (the replace strings), which would be supplied by the user when she installs. Is this a reasonable/possible thing to do using nsis?)

Thanks!


http://www.clantpa.co.uk/nsis/wiki/i...lacementScript

This script should do the trick. Just go line by line like the ReadFile script does and instead of comparing the line to a string, use this function on it.


Ignore the post plz... by the time i posted my ques...it was already answered above.. :(


Is there a way to use wildcards in the StrCmp instruction? because sometimes the line is not exactly the same but nsis has to replace it.


I doubt this can be done easily using NSIS scripting. If it is not always the same you will have to search for the parts that are the same using StrStr from functions.htm.