Skip to content
⌘ NSIS Forum Archive

Is it possible to do a search and replace on certain files?

42 posts

vbgunz#
Kichik, that last line of code takes care of the .old files as requested. Thank you so much for everything. Sorry for breaking earlier 🙁
vbgunz#
I know in my last post I said I have just one more question *but* I have just one more and I promise once this is solved my search and replace mission is over and I will move on.

I have many markers in my files in which require to be replaced with an absolute path (e.g. @@AbsolutePath@@). I was hoping I could take the absolute path from the "Destination Folder" field at the install location screen.

The trick though is not just getting the path from the "Destination Folder" but also reverting its "\" with "/"... I can get the user to enter this information manually but was hoping I could do it for them automatically.

So the Destination Folder looks like this...
C:\Program Files\My Application

But, when the user clicks install all markers in my files will then look like this...
C:/Program Files/My Application

I am really hoping this is not too hard. Please help me with just this last bit of code and I will be out of everyones hair in regards to search and replace.

Thank you so much as this is so important to me to get this last task out of the way.

Thank you
Joost Verburg#
Always search the Archive 🙂

Here it is: http://nsis.sourceforge.net/archive/...php?pageid=136
vbgunz#
Thank you Joost,

I insert this piece into my NSIS project script?
Push String to do replacement in (haystack)
Push String to replace (needle)
Push Replacement
Call StrRep
Pop $R0 result
And it is the below function I will save as an NSI script in my projects directory?

; Push $filenamestring (e.g. 'c:\this\and\that\filename.htm')
; Push "\"
; Call StrSlash
; Pop $0
; $0 == 'c:/this/and/that/filename.htm'
Function StrSlash
Exch $R3 ; $R3 = needle ("\" or "/")
Exch
Exch $R1 ; $R1 = String to replacement in (haystack)
Push $R2 ; Replaced haystack
Push $R4 ; $R4 = not $R3 ("/" or "\")
Push $R6
Push $R7 ; Scratch reg
StrCpy $R2 ""
StrLen $R6 $R1
StrCpy $R4 "\"
StrCmp $R3 "/" loop
StrCpy $R4 "/"
loop:
StrCpy $R7 $R1 1
StrCpy $R1 $R1 $R6 1
StrCmp $R7 $R3 found
StrCpy $R2 "$R2$R7"
StrCmp $R1 "" done loop
found:
StrCpy $R2 "$R2$R4"
StrCmp $R1 "" done loop
done:
StrCpy $R3 $R2
Pop $R7
Pop $R6
Pop $R4
Pop $R2
Pop $R1
Exch $R3
FunctionEnd
Can you please explain besides doing the above how I tie this into getting the value of the install directory and then applying that value to markers located through out various files?

I really am no geek but would very much like to use the NSIS installer as it allows me the freedom to customize the installation for end users. I believe it is very powerful...

Just how do I get the Directory Folder installation value, change all the "\" with "/" and then apply that value to markers through out the installation?

Please help me on this last task... I could ask the end user for the value but I trust NSIS to do it better.

Thank you for your help
Joost Verburg#
Example:
Push $INSTDIR
Push "\"
Call StrSlash
Pop $0
;Now $R0 contains the $INSTDIR with forward slashes
vbgunz#
Thank you so much Joost,

Before I run into error I just wish to confirm before moving ahead. Do I apply both codes...

The original snippet
Push String to do replacement in (haystack)
Push String to replace (needle)
Push Replacement
Call StrRep
Pop $R0 result
Your Snippet
Push $INSTDIR
Push "\"
Call StrSlash
Pop $0
;Now $R0 contains the $INSTDIR with forward slashes
or just your snippet instead?

Also do I save the function to an NSH and use it the same way I use the other files "StrReplace.nsh" and "ReplaceInFile.nsh"?

Thank you so much for your patience with me.
Joost Verburg#
There other ones are macro's. You don't have to use a macro for this one.

Use my snippet. Your snippet is for the generic replacer (read the page again).
vbgunz#
OK, I just need to confirm this before I find myself doing loops.

I use your snippet, not the other one. I save the function to a StrRep.nsh file in my project folder...

OK, once this is all done, how do I say replace this marker @@AbsolutePath@@ with $INSTDIR and your "SNIPPET", provided I've got the above mentioned solution correct so far?

Once I know a little more about the last step, I feel I can move ahead with experiment, otherwise I know I will simply be asking the same question in just a few moments.

I really am no geek and need to take it just a little slow. I hope you understand. I absolutely appreciate all the help you're giving me, I just need to know the best last step...

How to replace @@AbsolutePath@@ with $INSTDIR and your "SNIPPET"?

Thank you so much for your time I undoubtedly appreciate it all very much.
Joost Verburg#
Just use the variable ($0 in my example). Like kichik said:

!insertmacro ReplaceInFile "$INSTDIR\myotherfile.ini" "@marker@" "$0"

So you have to put the last snippet above this code.
vbgunz#
I just tried the following code...

; This snippet should help in replacing the Absolute system paths by replacing "\" with "/".
Push $INSTDIR
Push "\"
Call StrSlash
Pop $0
;Now $R0 contains the $INSTDIR with forward slashes
!insertmacro ReplaceInFile "$INSTDIR\crash.txt" "@AbsolutePath@" "$0"
But it keeps stopping at "Push $INSTDIR" with the following error "Error: Can't add entry, no section or function is open!"
vbgunz#
I've also tried the following
Section "Replace @AbsolutePath@"
; This snippet should help in replacing the Absolute system paths by replacing "\" with "/".
Push $INSTDIR
Push "\"
Call StrSlash
Pop $0
;Now $R0 contains the $INSTDIR with forward slashes
!insertmacro ReplaceInFile "$INSTDIR\crash.txt" "@AbsolutePath@" "$0"
SectionEnd
But I end up with the following error...
Adding plug-ins initializing function... Done!
Error: resolving install function "StrSlash" in section "Replace @AbsolutePath@" (5)
Note: uninstall functions must begin with "un.", and install functions must not
Error - aborting creation process
vbgunz#
I solved it Joost! I forgot to insert the !include... Excellent, Thank you so much for your time 🙂 Everyone will get a thank you in my credits 🙂

Thank you for putting up with me with so far

Thanks again