Backup files on install, restore on uninstall

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


Description

Here are 2 simple macros for backing up and restoring files.

Usage

!insertmacro BackupFile "Dir with file in it" "File" "Backup to"
!insertmacro RestoreFile "Backup dir with file in it" "File" "Restore to"

Example

Section "Install Files"
#Backup old copy
!insertmacro BackupFile "$INSTDIR" "blah1.nsi" "$INSTDIR\nsi-backup"
#Install new copy
 File "/oname=$INSTDIR" "blah1.nsi"
SectionEnd
 
Section "Uninstall"
#Remove installed copy
 Delete "$INSTDIR\blah1.nsi"
#Restore old copy
!insertmacro RestoreFile "$INSTDIR\nsi-backup" "blah1.nsi" "$INSTDIR"
SectionEnd

The Macros

!macro BackupFile FILE_DIR FILE BACKUP_TO
 IfFileExists "${BACKUP_TO}\*.*" +2
  CreateDirectory "${BACKUP_TO}"
 IfFileExists "${FILE_DIR}\${FILE}" 0 +2
  Rename "${FILE_DIR}\${FILE}" "${BACKUP_TO}\${FILE}"
!macroend
!macro RestoreFile BUP_DIR FILE RESTORE_TO
 IfFileExists "${BUP_DIR}\${FILE}" 0 +2
  Rename "${BUP_DIR}\${FILE}" "${RESTORE_TO}\${FILE}"
!macroend

-Stu

Note: By using above macros, create BackupFile.nsh and RestoreFile.nsh and save it nsis\include folder.
Then this should be included in your .nsi file as well.
example:!include BackupFile.nsh and !include RestoreFile.nsh

Note Added by Satish Kumar Barla: