Archive: Maintaining Ini files during upgrade


Maintaining Ini files during upgrade
Hi everyone,

I'm writting an installer/upgrader for an application which relies heavily on ini files to store config settings. I need the installer to be used for either a new installation or an upgrade - I don't want to have a separate installer for upgrades and another for new installations.

New installations are fine because I can simply create ini files with the default entries in.

My problem arises when I want to perform upgrades. I cannot recreate the entire ini file because it may overwrite some existing settings. So I was wondering, does anyone know if a function already exits similar to WriteINIStr that has an extra parameter that specifies whether to overwrite an existing entry in an ini file? This way I could ensure my existing settings do not get overwritten if they already exist, or force an overwrite if it does already exist. Or can anyone think of a better solution?

Many thanks


Just read from the INI first and check if the result is empty and/or error flag


Well that's essentially what I want to achieve. But I can't pheasibly do this for the hundreds of entries it needs to check - the script will be huuuuuge


I'm not sure if this is what you want:
http://nsis.sourceforge.net/Update_a...dated_INI_file

-Stu


Hi Stu

mmmmm not exactly what i'm after. That method would still overwrite my existing settings.

Here's a full example of what I'm aiming for:

Installer makes the following ini file with these default entries:

A=1
B=2
C=3

The program modifies these default settings to:

A=1a
B=2a
C=3a

Now, lets assume I want to run an upgrade at some later date. Let's say the program now uses D=4, so that needs to be added to the ini file. I don't want to overwrite the modifications that have been made to the existing entries (i'll have many unhappy users on my hand if I did this), so the end result is:

[Section]
A=1a
B=2a
C=3a
D=4

So basically, I want to do what Anders suggested and check if the entry already exists, if it does - skip it.

I know I could do this:

ReadINIStr $R0 "C:\myini.ini" "Section" "A"
StrCmp $R0 "" 0 +1
WriteINIStr "C:\myini.ini" "Section" "A" "1"


But bascially I don't want to have reems of code to check this, as it will treble the length of my script. Ideally what I'd like is this:

WriteINIStr /c "C:\myini.ini" "Section" "A" "1"

Where /c checks whether the entry already exists, if it does then skip it. I'm guessing something like that doesn't already exist, but I think it would be a useful addition. How difficult would it be to write a plugin?


Put it in a macro...


!macro WriteINIStrIfMissing File Section Key Value
ClearErrors
ReadINIStr $R0 "${File}" "${Section}" "${Key}"
IfErrors 0 +2
WriteINIStr "${File}" "${Section}" "${Key}" "${Value}"
!macroend
!define WriteINIStrIfMissing `!insertmacro WriteINIStrIfMissing`


Usage:
${WriteINIStrIfMissing} "myfile.ini" "Section" "Key" "Val"

-Stu

Ahhhh I've never used macros before so I never thought to use one - that's excellent, thanks for your help stu! :-)