Archive: GetFileTime Example


GetFileTime Example
  Hey, Basically I need to replace a file only if its newer than the existing one. I can't find any examples using the GetFileTime function. Some help would be appreciated.


Here's a small example:

It shows the functionallity of GetFileTime:

Use StrCmp $1 NEWTIME to check if other file or not.


OutFile "Setup.exe"
InstallDir '$PROGRAMFILES\Test'

LoadLanguageFile "../../Contrib/Language Files/German.nlf"
Caption "Test"
Name "Test"
DirText "Test"

Section "Test"
SetOutPath $INSTDIR
SectionEnd

Function .onInit
GetFileTime GetFileTime.nsi $0 $1
MessageBox MB_OK $0 ; file created
MessageBox MB_OK $1 ; file last changed
FunctionEnd


thanks for the example. I did something similar to try it but one of the files I tried returned a negative number. An example comparing two file would be more benifical. Thanks again though for your help.


GetFileTime returns two values that are actually one value split in half. There is the low half, and the high half. The high half is "worth" more, to get its real value you need to multiply it by 2^32. NSIS can only handle integers that can hold up to 32 bits, and can't hold this huge numbers, and that's why it is split in two. Because the high part is "worth" more you first need to compare it. If the high part is larger than the other high part the entire number is bound to be higher too. If the high parts are the same, then you need to compare the low parts.

I use IntCmpU because a DWORD is an unsigned long.

!define LOW1 $0

>!deinfe HIGH1 $1
>!define LOW2 $2
>!deinfe HIGH2 $3
GetFileTime${FILENAME} ${HIGH1} ${LOW1}
>GetFileTimeLocal "c:\\program files\\local file.dat" ${HIGH2} ${LOW2}
>IntCmpU ${HIGH1} ${HIGH2} 0 local_is_newer user_has_newer
; compare lows
IntCmpU${LOW1} ${LOW2} 0 local_is_newer user_has_newer
; files has the same time
Return

local_is_newer:
;user has an old file, install a newer version
File/oname=${FILENAME} "localfile.dat"
Return

user_has_newer:
; user has a newer version
MessageBox MB_OK "cheater!"