daemon.devin
daemon.devin

Reputation: 138

Update INI string using FindFirst

I'm trying to update an INI string that has something similar to [Version]DisplayVersion=0.0.298 from finding a new directory in a folder. The current INI string, 0.0.298, matches the current directory which looks like, ..\app-0.0.298.

During runtime the application will sometimes update itself creating a new folder which might look like, ..\app-0.0.301. What I want to do is find this directory and write its new version number to [Version]DisplayVersion to match the new updated version so it will look like this: [Version]DisplayVersion=0.0.301.

I have this so far which isn't working:

FindFirst $0 $1 `${APPDIR}\app-*`
ReadEnvStr $2 BUILD # Set earlier in the script ($2 = 0.0.298)
StrCmp $1 "" +11
Push `$2.0`
Push `$1.0`
Call VersionCompare # http://nsis.sourceforge.net/VersionCompare
Pop $3
IntCmp $3 1 +4 +4 0
IfFileExists `${APPDIR}\app-$1\${APP}.exe` 0 +3
DeleteINIStr "${InfoINI}" "Version" "DisplayVersion"
WriteINIStr "${InfoINI}" "Version" "DisplayVersion" "$1"
FindNext $0 $1
Goto -10
FindClose $0

What am I missing here or is there a better way to be doing this?

Upvotes: 0

Views: 203

Answers (1)

Anders
Anders

Reputation: 101736

Even if you pass a wildcard to FindFirst the returned filename will still include the entire name and you end up comparing 0.0.298.0 to app-0.0.298.0.

Section "-Initialize example"
!define APP "MyApp"
!define APPDIR "$temp\Test"
!define InfoINI "$temp\Test\app.ini"
CreateDirectory "${APPDIR}\app-0.0.298"
WriteINIStr "${InfoINI}" "Version" "DisplayVersion" "0.0.298"
System::Call 'KERNEL32::SetEnvironmentVariable(t "BUILD", t "0.0.298")'
SectionEnd

!include LogicLib.nsh

Page Components
Page InstFiles

Section "Emulate a update"
CreateDirectory "${APPDIR}\app-0.0.301"
File "/oname=${APPDIR}\app-0.0.301\${APP}.exe" "${__FILE__}"
SectionEnd


Section "Test"
SectionIn RO
FindFirst $0 $1 `${APPDIR}\app-*`
ReadEnvStr $2 BUILD # Set earlier in the script ($2 = 0.0.298)
loop:
    StrCmp $1 "" done
    StrCpy $3 $1 4
    StrCmp $3 "app-" 0 trynext
    StrCpy $1 $1 "" 4 ; Remove "app-" prefix
    Push `$2.0`
    Push `$1.0`
    Call VersionCompare # http://nsis.sourceforge.net/VersionCompare
    Pop $3
    ${If} $3 > 1
        ${If} ${FileExists} "${APPDIR}\app-$1\${APP}.exe"
            # DeleteINIStr "${InfoINI}" "Version" "DisplayVersion" ; You don't have to delete before writing
            WriteINIStr "${InfoINI}" "Version" "DisplayVersion" "$1"
        ${EndIf}
    ${EndIf}
    trynext:
        FindNext $0 $1
    Goto loop
done:
FindClose $0
SectionEnd

Tip: Using relative jumps makes the code harder to read (and modify), use labels and/or LogicLib.nsh

Upvotes: 1

Related Questions