Archive: Replace old files


Replace old files
Hi.
Im on the moment creating an installer for my new work. This installer will overwrite an file which ist existing on the HDD.
The old file should be saved by the installer and the uninstaller should replace it if the programm will be uninstalled.
How i do that?
Thanks


So you want to make a backup on install and restore the file on uninstall?
Insert these 2 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

install section:
Section "Test" 
#Backup old copy
!insertmacro BackupFile "$INSTDIR\Path\To\File\" "file.dll" "$INSTDIR\Path\To\Backup\Folder\"
#Install new file
SetOutPath "$INSTDIR\Path\To\File\"
File "file.dll"
SectionEnd

uninstall section:
Section Uninstall
#Remove installed copy
Delete "$INSTDIR\Path\To\File\file.dll"
#Restore old copy
!insertmacro RestoreFile "$INSTDIR\Path\To\Backup\Folder\" "file.dll" "$INSTDIR\Path\To\File\"
SectionEnd

The example is simple, but should show, what is needed.

Thx for your fast help.
Here's my next question:

;Installer Sections  
Section "Main Files" SecMain
SetOutPath "$INSTDIR\System\"
;myfile.exe


At compiling i get this error:

Section: "Main Files" ->(SecMain)
SetOutPath: "$INSTDIR\System\"
Error: command Section not valid in Section
Error in script "C:\Programme\NSIS\Examples\Modern UI\WelcomeFinish.nsi" on line 57 -- aborting creation process

"Error: command Section not valid in Section"
Use this:

;Installer Sections  
Section "Main Files" SecMain
SetOutPath "$INSTDIR\System\"
File "my file.exe"
SectionEnd

Cannot say anything to the other compile error, because for that,I must see more from your script.

Next error:

Section: "Bandar" ->(files)
Error: "files" already defined, can't assign section index!
Error in script "C:\Programme\NSIS\Examples\Modern UI\WelcomeFinish.nsi" on line 72 -- aborting creation process


Section Bandar files    
SetOutPath "$INSTDIR\***\Bandar"
File ***.dat
File *****.dat
SectionEnd

You probably just missed the quotations

Section "Bandar files"    
SetOutPath "$INSTDIR\***\Bandar"
File "***.dat"
File "*****.dat"
SectionEnd

Plus, you cannot define one section description for 2 sections, e.g.

Section "Blah" SECBLAH
SetOutPath "$INSTDIR\***\Blah"
File "***.dat"
SectionEnd

Section "BlahBlah" SECBLAH
SetOutPath "$INSTDIR\***\BlahBlah"
File "***.dat"
SectionEnd

doesn't work, because the first section already uses it.

OK, last question:
My installer installs everytime the same 2 files in 20/30 directorys. What i have to do that the uninstaller deletes this 2 files in every folder but not the other files in this directory?
Thanks


You have to delete every single file:

Section Uninstall
;Delete files
Delete "$INSTDIR\Blah\Blah1.exe"
Delete "$INSTDIR\Blah\Blah2.exe"
Delete "$INSTDIR\Blah1\Blah1.exe"
Delete "$INSTDIR\Blah1\Blah2.exe"
Delete "$INSTDIR\Blah2\Blah1.exe"
Delete "$INSTDIR\Blah2\Blah2.exe"
SectionEnd

and so on.

that would take me a long time to do :)
but if there's no other solution...
Thanks 4 help


Simply Copy and Paste will do most work. ;)

Maybe there is a shorter ability too, don't know, I'm always deleting every single file, just to make sure, that uninstaller works as expected.

If you install files to your own folder, you could use *.*, but i havn't tried it in any of my installer scripts yet.

If you're installing files in other folders, such as e.g. c:\Windows, you probably should use the long way, to make sure, that you don't uninstall other files by mistake.