Replace File On Reboot

From NSIS Wiki
Jump to navigationJump to search
Author: Shane (talk, contrib)


Description

Replace an existing file on reboot (for replacing files in-use, etc.).

The Script

!define PRODUCT_CODE "MyProductCode"
 
RequestExecutionLevel admin
ShowInstDetails show 
 
; The name of the installer
Name "ReplaceOnReboot"
 
; The file to write
OutFile "ReplaceOnReboot.exe"
 
; Get the existing product installation directory - use that for the INSTDIR value
InstallDirRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_CODE}" "InstallLocation"
 
 
Section "" ;No components page, name is not important
	StrCmp $INSTDIR "" 0 +2 ; If the INSTDIR is empty, we need to abort
	Abort 
 
	;Stage the files in the Windows\Temp directory until the next reboot
	;then call MoveFileEx to move them after the next reboot
	CreateDirectory "$WINDIR\Temp\${PRODUCT_CODE}" 
	SetOutPath "$WINDIR\Temp\${PRODUCT_CODE}"
 
	File "test.dll"
 
	;Call MoveFileEx on each file above (Params: <source>, <destination>, 4) 5 == Move on Reboot && Replace Existing
	System::Call "kernel32::MoveFileEx(t '$WINDIR\Temp\${PRODUCT_CODE}\test.dll', t '$INSTDIR\test.dll', i 5)"
 
SectionEnd ; end the section