Backup files on install, restore on uninstall

From NSIS Wiki

Author: Afrow UK (talk, contrib)


Contents

[edit] Description

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

[edit] Usage

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

[edit] 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

[edit] 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

donate
ads