- NSIS Discussion
- substitute stings in files
Archive: substitute stings in files
n0On3
13th October 2002 18:17 UTC
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:)
kichik
13th October 2002 18:49 UTC
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]
Freeman II
15th October 2002 03:00 UTC
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
kichik
15th October 2002 15:58 UTC
Please attach large scripts next time. Script attached here.
ReadFile should be FileRead.
n0On3
15th October 2002 16:38 UTC
kichik, it worked nicely for me.
It's nice piece of code.
Freeman II
16th October 2002 06:20 UTC
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!
n0On3
16th October 2002 15:06 UTC
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
>
kichik
16th October 2002 16:04 UTC
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 ;)
Freeman II
17th October 2002 00:31 UTC
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?
kichik
17th October 2002 13:38 UTC
Add Delete $R0 after CopyFiles. I have updated the thread again.
n0On3
19th October 2002 14:28 UTC
For the benefit of other users, I suggest to add an exemple of this in the nsis package.
pviton
19th October 2002 19:57 UTC
(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!
kichik
19th October 2002 20:11 UTC
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.
pinku41
19th October 2002 20:17 UTC
Ignore the post plz... by the time i posted my ques...it was already answered above.. :(
n0On3
20th October 2002 12:37 UTC
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.
kichik
20th October 2002 18:00 UTC
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.