Archive: Need to edit two files after installation (search & replace)


Need to edit two files after installation (search & replace)
  Dear Community,

I have started a project for installing a complete WAMP (webserver with PHP and MySQL). After successfully copying the files to their destination, two files have to be edited. The first is the webservers-configfile, the other one is the php-configurationfile. In these two files I have some directives which correspond to the installation-path. So I want to deliver a modified conf-file with one variable called (e.g.) @@SERVERROOT@@. After installation is completed, I need to search for that string and replace it with "$INSTDIR\path\to\program".

How can this be achieved/solved?

Another question:
Does anybody know how to search for an existing service and if this exists to make the installer stop (better open a pre-definied textfile with a solution to solve this problem "and go on with the installation after solving")?

Cheers
Mosestycoon


To edit files use FileOpen, FileWrite, FileRead, FileSeek and FileClose. Go to this for a good example. It replaces certain line in a text file with another line. You can set in the config file you are installing a certain line and then have this little script replace it with whatever you want.

To find and stop services use services.dll by Sunjammer from:
http://nsis.sourceforge.net/archive/download.php


I am really surprised. For even the most complicated things (at least to me) is something to solve it. The answer is even shorter than the question!


Thx, I will give it a try :-)

Mosestycoon

P.S. Where do I have to place it?


It's not really commended well, so as I'm a really bad programmer, this is not simple for me ;-(


Put that code after the file extraction code. Maybe it a section of its own called "edit config files", maybe right after the config extraction, that's up to you.


I tried it with this function:


FileOpen $0 "$INSTDIR\Apache2\conf\httpd.pre.conf" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop:
FileRead $0 $2
IfErrors done
StrCmp $2 "ServerRoot @@SERVERROOT@@/Apache2$\r$\n" 0 +3
FileWrite $1 "ServerRoot $INSTDIR/Apache2$\r$\n"
Goto loop
; StrCmp $2 "line to replace" 0 +3
; FileWrite $1 "replacement of line"
; Goto loop
FileWrite $1 $2
Goto loop
done:
FileClose $0
FileClose $1
Delete "$INSTDIR\Apache2\conf\httpd.pre.conf"
Rename $R0 "$INSTDIR\Apache2\conf\httpd.conf"


BUT the compiler said:

Processed 1 file, writing output:
Adding plug-ins initializing function... Done!
warning: install function ".ReplaceString" not referenced - zeroing code (4182-4196) out

So this is a bit suspect, because the installer should "search & replace", not the compiler...

Am I wrong?

I need this to be parsed on clients, after installation.

A possibility would be to parse this through sed.exe (streaming-editor), but I have no idea where to get the installation-path from so that this could be taken as parameter for sed.exe

The problem with your code is that regular functions are called if you don't call them. Sections are called according to the user's choice in the components page, but functions have to be called from sections by you. For example:


myFunc

># do stuff....
>FunctionEnd

Section "Do stuff"
># extract some files...
# write do the registry...
>Call myFunc
SectionEnd
>
There is another type of functions, call-back function, that are called by the installer, but you shouldn't care about those now.

Ok, after a good movie and some tests I think I got it.

Thx 4 help