Archive: Backup Files at install if file exsits..?


Backup Files at install if file exsits..?
When installing stuff, some files might get overwritten on the end user's pc. Is there a way to make the installer move files to be overwritten to a backup folder, so at uninstall, all is as if the installer never was installed?


It can be hard to do this.

Example:
CreateDirectory "$INSTDIR\backup"
IfFileExists "$INSTDIR\file.txt" 0 +2
Rename "$INSTDIR\file.txt" "$INSTDIR\backup\file.txt"

SetOutPath $INSTDIR
File "file.txt"

Then for uninstall, just do
CopyFiles "$INSTDIR\backup\*.*" "$INSTDIR\*.*"
RMDir /r "$INSTDIR\backup"

-Stu


Ok I get the idea.. thx m8.. u r a plain old dude of knowleg ;)

This is my file as it is of now, but I can't get it to uninstall right. All it sais is "Copy failed". Anyone how can see what's wrong?

UninstallText "This will uninstall xes ${version}${build}. Hit next to continue."
Section "Uninstall"
SetAutoClose false
ReadRegStr $R0 HKLM Software\x-systems\xes InstallDir
CopyFiles $R0\cstrike\xes_backup\*.* $R0\cstrike\*.*
RMDir /r $R0\cstrike\xes_backup
RMDir /r $R0\cstrike\xes
RMDir /r $SMPROGRAMS\x-systems\xes
RMDir $SMPROGRAMS\x-systems
DeleteRegKey HKLM Software\Microsoft\Windows\CurrentVersion\Uninstall\x-systems\xes
DeleteRegKey /ifempty HKLM Software\Microsoft\Windows\CurrentVersion\Uninstall\x-systems
DeleteRegKey HKLM Software\x-systems\xes
DeleteRegKey /ifempty HKLM Software\x-systems
SectionEnd


I'v tryed to make the uninstaller reinstall the overwritten files by doing this:

UninstallText "This will uninstall xes ${version}${build}. Hit next to continue."
Section "Uninstall"
SetAutoClose false
ReadRegStr $INSTDIR HKLM Software\x-systems\xes InstallDir
IfFileExists "$INSTDIR\cstrike\xes_backup\autoexec.cfg" +1 +2
Rename "$INSTDIR\cstrike\xes_backup\autoexec.cfg" "$INSTDIR\cstrike\autoexec.cfg"

RMDir /r $INSTDIR\cstrike\xes_backup
RMDir /r $INSTDIR\cstrike\xes
RMDir /r $SMPROGRAMS\x-systems\xes
RMDir $SMPROGRAMS\x-systems
DeleteRegKey HKLM Software\Microsoft\Windows\CurrentVersion\Uninstall\x-systems\xes
DeleteRegKey /ifempty HKLM Software\Microsoft\Windows\CurrentVersion\Uninstall\x-systems
DeleteRegKey HKLM Software\x-systems\xes
DeleteRegKey /ifempty HKLM Software\x-systems
SectionEnd

But in the details window this is all that is shown:

Remove folder: D:\Half-Life\cstrike\xes_backup
Remove folder: D:\Half-Life\cstrike\xes
Remove folder: C:\Documents and Settings\Administrator\Start Menu\Programs\x-systems\xes
Remove folder: C:\Documents and Settings\Administrator\Start Menu\Programs\x-systems
Completed

Any ideas folks?


Whats wrong?

Write/ReadRegStr's or any form of INI/Reg management in NSIS is not shown in the LogWindow.
Why would the user want to see that kind of stuff - the LogWindow is to show management of file movement/extraction.
-Stu


Your first examble didn't work, it just wrote "Copy files from ?:\blah\more blah\*.* to ?:\new blah -> Copy failed.

Originally posted by Afrow UK
Then for uninstall, just do
CopyFiles "$INSTDIR\backup\*.*" "$INSTDIR\*.*"
RMDir /r "$INSTDIR\backup"

-Stu
So I tryed to make it like this:

IfFileExists "$INSTDIR\cstrike\xes_backup\autoexec.cfg" +1 +2
Rename "$INSTDIR\cstrike\xes_backup\autoexec.cfg" "$INSTDIR\cstrike\autoexec.cfg"


But it still doesn't work. The files that are backed up, are not reinstalled at uninstall.

Try using my code with:

CopyFiles "$INSTDIR\backup\*.*" "$INSTDIR"


Sorry Afrow but I think you need to do some more sleeping ;P

Your first examble didn't work, it just wrote "Copy files from ?:\blah\more blah\*.* to ?:\new blah -> Copy failed.



Originally posted by Afrow UK
Then for uninstall, just do
CopyFiles "$INSTDIR\backup\*.*" "$INSTDIR\*.*"
RMDir /r "$INSTDIR\backup"

-Stu


My method is far better than the method you are trying to achieve.
Firstly, you are backing up the files - thats fine, but on uninstall, why copy a single file back when you can copy them all back in one go.

E.g.
#Install
Rename "C:\blah1.txt" "C:\backup\blah1.txt"
Rename "C:\blah2.txt" "C:\backup\blah2.txt"
Rename "C:\blah3.txt" "C:\backup\blah3.txt"

#Uninstall
CopyFiles "C:\backup\blah1.txt" "C:\blah1.txt"
CopyFiles "C:\backup\blah2.txt" "C:\blah2.txt"
CopyFiles "C:\backup\blah3.txt" "C:\blah3.txt"

The uninstall bit is pointless and a waste of code.
A simple
CopyFiles "C:\backup\*.*" "C:\"
Will do them all in one go.

-Stu


It can be hard to do this.

Example:
CreateDirectory "$INSTDIR\backup"
IfFileExists "$INSTDIR\file.txt" 0 +2
Rename "$INSTDIR\file.txt" "$INSTDIR\backup\file.txt"

SetOutPath $INSTDIR
File "file.txt"

Then for uninstall, just do
CopyFiles "$INSTDIR\backup\*.*" "$INSTDIR"
RMDir /r "$INSTDIR\backup"

-Stu

This is what happens when I try your method:

Copy to D:\Half-Life\cstrike\xes\*.*
Copy failed
Remove folder: D:\Half-Life\cstrike\xes_backup
Remove folder: D:\Half-Life\cstrike\xes
Remove folder: C:\Documents and Settings\Administrator\Start Menu\Programs\x-systems\xes
Remove folder: C:\Documents and Settings\Administrator\Start Menu\Programs\x-systems
Completed

And the problem is this: Copy to D:\Half-Life\cstrike\xes\*.*. For some reason it puts a \xes\ folder in the $INSTDIR or where ever, I don't know. This is the code I have in the script:
CopyFiles "$INSTDIR\xes_backup\*.*" "$INSTDIR\*.*"

I might add that it delets the folders and removes the reg's and shortcuts and so on..


I've already told you twice:
CopyFiles "$INSTDIR\xes_backup\*.*" "$INSTDIR\*.*"
should be
CopyFiles "$INSTDIR\xes_backup\*.*" "$INSTDIR"

-Stu


Yes I know.. it still makes no difference :(

Copy to D:\Half-Life
Copy failed
Remove folder: D:\Half-Life\cstrike\xes_backup
Remove folder: D:\Half-Life\cstrike\xes
Remove folder: C:\Documents and Settings\Administrator\Start Menu\Programs\x-systems\xes
Remove folder: C:\Documents and Settings\Administrator\Start Menu\Programs\x-systems
Completed


Can you show the exact script like it is now?

Btw, how many files are you backing up?

Thanks

-Stu


I don't know how to attach..? About 10-13 files plus the folder tree they are in.


I mean the uninstall code.


UninstallText "This will uninstall xes ${version}${build}. Hit next to continue."
Section "Uninstall"
SetAutoClose false
ReadRegStr "$INSTDIR" HKLM Software\x-systems\xes InstallDir
CopyFiles "$INSTDIR\xes_backup\" "$INSTDIR\cstrike\"
RMDir /r "$INSTDIR\cstrike\xes_backup"
RMDir /r "$INSTDIR\cstrike\xes"
RMDir /r $SMPROGRAMS\x-systems\xes
RMDir $SMPROGRAMS\x-systems
DeleteRegKey HKLM Software\Microsoft\Windows\CurrentVersion\Uninstall\x-systems\xes
DeleteRegKey /ifempty HKLM Software\Microsoft\Windows\CurrentVersion\Uninstall\x-systems
DeleteRegKey HKLM Software\x-systems\xes
DeleteRegKey /ifempty HKLM Software\x-systems
SectionEnd

The "$INSTDIR\cstrike\" was just an atempt to find the error.. I'v tryed "$INSTDIR", "$INSTDIR\", "$INSTDIR\*.*" and so on and on....
It just returns "Copy faild" :(


CopyFiles "$INSTDIR\xes_backup\*.*" "$INSTDIR" should work fine.

If you are only backing up 1 file then you could use your code:

IfFileExists "$INSTDIR\xes_backup\autoexec.cfg" 0 +3
Delete "$INSTDIR\autoexec.cfg"
Rename "$INSTDIR\xes_backup\autoexec.cfg" "$INSTDIR\autoexec.cfg"
RMDir "$INSTDIR\xes_backup"

-Stu


How about this:
CopyFiles "$INSTDIR\xes_backup\*.*" "$INSTDIR\*"

-Stu


If the file is already there, does CopyFiles not overwritte it?

How about this:
CopyFiles "$INSTDIR\xes_backup\*.*" "$INSTDIR\*"

Nop didn't change a thing.. damn one should think that the NSIS team would have made a better uninstall function.. or some more up-2-date documentation ;P


Yes, unless the file is open or being used.

You could do a
Delete "$INSTDIR\autoexec.cfg"
before hand.


I think this now is a better method.
It allows more control over the files...

IfFileExists "$INSTDIR\xes_backup\autoexec.cfg" 0 +3
Delete "$INSTDIR\autoexec.cfg"
Rename "$INSTDIR\xes_backup\autoexec.cfg" "$INSTDIR\autoexec.cfg"

IfFileExists "$INSTDIR\xes_backup\blah.txt" 0 +3
Delete "$INSTDIR\blah.txt"
Rename "$INSTDIR\xes_backup\blah.txt" "$INSTDIR\blah.txt"

RMDir "$INSTDIR\xes_backup"

-Stu


YES! If there is a file with the same name as the target file as in the Rename label is doesn't move it! *hint* put in documentation *hint* :)

Thx Afrow.. now it does what I want it to.. thx alot m8 :)