2017-08-23 30 views
0

我试图更新一个INI字符串,它具有类似[Version]DisplayVersion=0.0.298的功能,以查找文件夹中的新目录。当前的INI字符串0.0.298与当前目录相匹配,如..\app-0.0.298使用FindFirst更新INI字符串

在运行期间,应用程序有时会更新自己,创建一个新文件夹,可能看起来像..\app-0.0.301。我想要做的是找到这个目录,并将其新版本号写入[Version]DisplayVersion以匹配新的更新版本,所以它看起来像这样:[Version]DisplayVersion=0.0.301

我有这个迄今为止未工作:

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 

缺少什么我在这里还是有更好的方式来这样做呢?

+0

什么不起作用?目录枚举? VersionCompare? Ini处理? APPDIR和InfoINI定义的是什么? – Anders

回答

1

即使您将通配符传递给FindFirst,返回的文件名仍将包含整个名称,并最终将0.0.298.0app-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 

提示:使用相对跳转使得代码难以阅读(和修改),使用标签和/或LogicLib.nsh

+0

我应该知道更好,但如果我没有,现在我知道了。谢谢安德斯。当我看不到某些东西时,我通常需要另一双眼睛,而且你的眼睛总是为我而来。 –