Archive: Replace \ with \\


Replace \ with \\
Hello

I want to replace a placeholder in a file with the InstDir .
But the path should be with double backslashes.
e.g.
C:\\Program

I tried this:

Push "$INSTDIR" ;original string
Push "\" ;needs to be replaced
Push "\\" ;will replace wrong characters
Call StrReplace
Pop $0

Then MessageBox MB_OK $0 shows a lot of \\\\\\\\\\\\\\\\\\\\\\\\\\

what did I wrong or how can I do this?
regards
hawk


I guess when the first backslash is replaced, and the next character is checked, it'll see a backslash again, causing a infinite loop.
You could first replace the backslashes with another character, like a hash, and then replace that character with a double backslash.


Done
Hello,
thanks a lot for your help.

Originally posted by jpderuiter
I guess when the first backslash is replaced, and the next character is checked, it'll see a backslash again, causing a infinite loop.
You could first replace the backslashes with another character, like a hash, and then replace that character with a double backslash.
Yes, good idea.
I did it in this way:
Push "$INSTDIR" ;original string
Push "\" ;needs to be replaced
Push "#" ;will replace wrong characters
Call StrReplace
Pop $0

Push $0 ;original string
Push "#" ;needs to be replaced
Push "\\" ;will replace wrong characters
Call StrReplace
Pop $0

It works now. No idea if you can do this easier.

regards
hawk

Originally posted by hawkmaster
No idea if you can do this easier.
Of course you can, but you'd need to edit the StrReplace function (or write your own, maybe as a macro to get rid of the push/pops).

!include "MUI2.nsh"
!include "WordFunc.nsh"

Name "PROGRAM"
OutFile "Setup.exe"
InstallDir "$TEMP"

ShowInstDetails show
!insertmacro WordReplace
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"

Section installl

StrCpy $0 "C:\\Program Files\\Internet Explorer\\iexplore.exe"
DetailPrint "original: $0"
${WordReplace} $0 "\\" "\" "+" $0
DetailPrint "replaced: $0"

Sectionend