2017-08-23 28 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

我應該知道更好,但如果我沒有,現在我知道了。謝謝安德斯。當我看不到某些東西時,我通常需要另一雙眼睛,而且你的眼睛總是爲我而來。 –