Skip to content
⌘ NSIS Forum Archive

Problems with line replacement

6 posts

treaz#

Problems with line replacement

Hi,

I am having a problem with my $USER_INSTALL_DIR$ in the code below.

When I tried $$USER_INSTALL_DIR$$, it doesn't work (in fact it wiped out my whole file and returned me a blank file instead).

Could someone help please. Thanks.

This is the code:


FileOpen $0 "$INSTDIR\test.txt" "r"
GetTempFileName $R3
FileOpen $1 $R3 "w"

loop:
FileRead $0 $2
IfErrors done
StrCmp $2 "$USER_INSTALL_DIR$" 0 +3
FileWrite $1 "$INSTDIR"
Goto loop
FileWrite $1 $2
Goto loop
done:
FileClose $0
FileClose $1
Delete "$INSTDIR\test.txt"
Rename $R3 "$INSTDIR\test.txt"

kichik#
If NSIS can't find the variable (as in your case) it will not convert the dollar sign ($) and spit up a warning. If you put two dollars, it will not spit up a warning and will not try to convert it to a variable.

That's not the problem here. The problem is that you forgot $\r$\n in the string.

I have updated my code in the archive (http://nsis.sourceforge.net/archive/....php?pageid=17) to check for unbroken lines too (at the end of the file for example). Try it.
kichik#
BTW, you don't have to color your code manually, just put it between [ php ] and [ /php ] (without those spaces) and it will be colored and indented automatically.
treaz#
Hi,
May I know if your code checks for words within a line and replace them? Could that be possible?

Thanks.
kichik#
This specific script doesn't, but it can be easily changed to use StrReplace on every line. This was talked on the forum a few days ago, you should be able to find some examples I think.
kichik#
FileOpen $0 "file.txt" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop:
   FileRead $0 $2
   IfErrors done
   Push $2 #original string
   Push "string to replace" #needs to be replaced
   Push "string to replace with" #will replace wrong characters
   Call StrReplace
   Pop $2
   FileWrite $1 $2
   Goto loop
done:
   FileClose $0
   FileClose $1
   Delete "file.txt"
   Rename $R0 "file.txt"