- NSIS Discussion
- Replace old files
Archive: Replace old files
DaOmAsTeR
12th April 2008 23:39 UTC
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
Koopa
12th April 2008 23:51 UTC
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.
DaOmAsTeR
12th April 2008 23:53 UTC
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
Koopa
12th April 2008 23:57 UTC
"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.
DaOmAsTeR
13th April 2008 00:11 UTC
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
Koopa
13th April 2008 00:15 UTC
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.
DaOmAsTeR
13th April 2008 00:24 UTC
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
Koopa
13th April 2008 00:29 UTC
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.
DaOmAsTeR
13th April 2008 00:30 UTC
that would take me a long time to do :)
but if there's no other solution...
Thanks 4 help
Koopa
13th April 2008 00:33 UTC
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.