- NSIS Discussion
- More text replacement woes...
Archive: More text replacement woes...
moop
5th October 2004 02:32 UTC
More text replacement woes...
I have read the docs. I have been searching these forums for hours. I still need help. I have installed and used the StrReplace and ReplaceInFile scripts from the Archive. I'm not a programmer by day, so bear with me, please. The closest solution to what I've found so far is HERE.
Basically, I want to do two things. I'll wait before posting the second one because hopefully I can learn how to do it from the response I get to this one.
----my text file----
asdfasdf
CDPath=F:
asdfasdf
----eof-------------
Wherein F: can be anything, from blank, to a longer string.
I have seen how to replace a full line, but not unless I match the string exactly. How can I flush the junk after the equals sign and keep the rest intact? I have used the ReplaceInFile !insertmacro, but that only works if my string matches exactly, and although I can "cheat" by inserting a $\r$\n, that still just moves the "junk" after the equals sign to another line I would still need to delete.
I'm not a programmer by day, please forgive my noobishness... This isn't second nature to me yet, and I'm certain a NSIS whiz could do what I want in 5 minutes, which has taken me... longer than I care to admit.
Afrow UK
5th October 2004 18:01 UTC
I think this calls for another text manipulation function by Afrow!
Please bare with me and I will have one written shortly.
-Stu
Afrow UK
5th October 2004 18:38 UTC
Done:
http://nsis.sourceforge.net/archive/...b.php?page=626
You want to do this:
Push "$INSTDIR\file.ext"
Push "CDPath="
Push "CDPath=new value"
Call ReplaceLineStr
-Stu
moop
5th October 2004 20:40 UTC
Thanks a ton. I had gone to the IRC chatroom yesterday where Anders gave me some useful code, but most of it was greek. I still want to use it to learn though, but yours is immediately helpful. Thanks very much, Afrow! I hope I can help my community as much as you help NSIS. :cool:
razor_x
6th October 2004 18:17 UTC
sweet afrow..can this be made into a macro too?
Afrow UK
6th October 2004 19:03 UTC
...
!macro ReplaceLineStr File StartStr LineStr
Push "${File}"
Push "${StartStr}"
Push "${LineStr}"
Call ReplaceLineStr
!macroend
!insertmacro ReplaceLineStr "$INSTDIR\file.ext" "CDPath=" "CDPath=new value"
-Stu
Kolya
3rd March 2010 20:26 UTC
Sorry for bumping such an old thread...
This function has been very helpful but it misses one important feature for me: If the searched string isn't found, I'd like to write the replacement string to a new line, instead of just dropping it.
I know how to add the new line at the bottom of the file, but I need help with writing that exception.
Kolya
4th March 2010 23:22 UTC
Some help please? Or am I missing something obvious?
Kolya
5th March 2010 17:09 UTC
Nevermind, I eventually used the script to delete any previously existing matching lines and then added my lines at the bottom.
This worked for me because it doesn't matter where the new lines are.
Kolya
24th March 2013 19:54 UTC
Eventually I did need a solution that adds the new line to the bottom if it isn't found in the file. It also echoes in the details how many times which line was replaced in which file or if it was added to he bottom.
I'm sure this could be done more elegantly, but it was the first time I screwed with NSIS functions.
Function ReplaceLineStr
Exch $R0 ; string to replace that whole line with
Exch
Exch $R1 ; string that line should start with
Exch
Exch 2
Exch $R2 ; file
Push $R3 ; file handle
Push $R4 ; temp file
Push $R5 ; temp file handle
Push $R6 ; global
Push $R7 ; input string length
Push $R8 ; line string length
Push $R9 ; global
Var /GLOBAL T0 ; counter
StrCpy $T0 0 ; reset
StrLen $R7 $R1
GetTempFileName $R4
FileOpen $R5 $R4 w
FileOpen $R3 $R2 r
ReadLoop:
ClearErrors
FileRead $R3 $R6
IfErrors Done
StrLen $R8 $R6
StrCpy $R9 $R6 $R7 -$R8
StrCmp $R9 $R1 0 +4
FileWrite $R5 "$R0$\r$\n"
IntOp $T0 $T0 + 1
Goto ReadLoop
FileWrite $R5 $R6
Goto ReadLoop
Done:
FileClose $R3
FileClose $R5
SetDetailsPrint none
Delete $R2
Rename $R4 $R2
SetDetailsPrint both
${If} $T0 != 0
DetailPrint "$\"$R1$\" replaced $T0 times with $\"$R0$\" in $\"$R2$\"."
${Else}
FileOpen $T0 $R2 a
FileSeek $T0 0 END
FileWrite $T0 $R0
FileWriteByte $T0 "13"
FileWriteByte $T0 "10"
FileClose $T0
DetailPrint "$\"$R0$\" not found in $\"$R2$\", added to bottom of file."
${EndIf}
Pop $R9
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd