z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

How to backup and restore files/folders in APPDIR?

In a same project, how to backup files/folders in APPDIR at Install, and then restore this backup to APPDIR at Uninstall?

Or, Create operation of move files in APPDIR to Backup folder at Install (This has been easy to achieve), and then move these files to APPDIR at Uninstall?

In "How do I prevent a file from being replaced by a newer version?" page in help of AD, The Sample projects is to backup files/folders in APPDIR at Uninstall, and restore this backup to APPDIR at Install by two projects. This Note is very difficult to understand.

Please help me, Thanks
Last edited by z_shangyi on Wed Oct 29, 2008 12:04 pm, edited 1 time in total.
Ionut
Posts: 605
Joined: Tue Nov 22, 2005 11:29 am
Contact: Website

Re: How to backup and restore files/floders in APPDIR?

Hi Zhang Shangyi,

You can use a Custom Action to copy (or move) the files/folders in question to a backup folder. You can use the sample "Backup" script for this purpose because file copy/move operations cannot be used to copy folders. The path of the backup folder will be written to the registry (by creating a registry value in the Registry page). Use the condition (Not Installed) for the Backup Custom Action.

You will also create a Search in your project for the backup folder which will be used by the Restore Custom Action. You can use the "xcopy" command in order to restore all the files/folders from the backup folder to the Application Folder. Use the condition (REMOVE = "ALL") in order to condition the Restore Custom Action.

Hope this helps.

Regards,
Ionut
Denis Toma
Advanced Installer Team
http://www.advancedinstaller.com/
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: How to backup and restore files/folders in APPDIR?

Thank you help me!
Ionut wrote: You can use a Custom Action to copy (or move) the files/folders in question to a backup folder. You can use the sample "Backup" script for this purpose because file copy/move operations cannot be used to copy folders. The path of the backup folder will be written to the registry (by creating a registry value in the Registry page). Use the condition (Not Installed) for the Backup Custom Action.
This sample "Backup" script will to backup files/folders to a temp folder under currently user's TempFolder. But can't transfer this property to use at uninstall. The need to written "tfolder" property to the registry, and then to create a bACKUPTEMPFOLDER search for get this property, and set Action Data of Restore Custom Action as [BACKUPTEMPFOLDER]|[BackupList]<>[RestoreLocationList] before it can be used to restore (use sample "Restore" script). How to modify this script in order to I can to customize a backup folder (Because files/folders under temporary directory may be deleted by the user!)? Can modify this script for me (I do not know the script)?
Ionut wrote: You will also create a Search in your project for the backup folder which will be used by the Restore Custom Action. You can use the "xcopy" command in order to restore all the files/folders from the backup folder to the Application Folder. Use the condition (REMOVE = "ALL") in order to condition the Restore Custom Action.
Use the "xcopy" command can restore all the files/folders from the backup folder. but can't automatically delete the backup folders. This is not a good method.

Thanks!

Zhang Shangyi
Last edited by z_shangyi on Wed Oct 29, 2008 3:30 pm, edited 12 times in total.
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: How to backup and restore files/folders in APPDIR?

I wait for your revised script. Thank you!
Ionut
Posts: 605
Joined: Tue Nov 22, 2005 11:29 am
Contact: Website

Re: How to backup and restore files/folders in APPDIR?

Hi Zhang Shangyi,

I have modified the Backup script so that it can also be executed as Deferred (in system context), in which case you have the possibility to specify the backup folder yourself.

When not using the TempFolder as the backup folder, the Backup Custom Action must be set as "Deferred with no impersonation" - otherwise it might fail in Vista when attempting to write to the designated backup folder. When the Backup Custom Action is executed as Deferred, data is passed to it via the "Action Data" field, in the following format:

Code: Select all

[BackupFolder]<>[BackupList]
The modified VBS file and sample project are attached below. Some notes regarding the example project:

1. I have used the folder "[CommonAppDataFolder][ProductName]" as the backup folder. This usually is "C:\Documents and Settings\All Users\Application Data\[ProductName]" on Win2000 and XP, or "C:\ProgramData\[ProductName]" on Vista. You can change it to whatever is appropriate in your case.

2. The path of the backup folder is saved in the registry under the key HKLM\[Manufacturer]\[ProductName] (see the Registry page) such that it can be retrieved from there by a Search (named BACKUP_FOLDER - see the Search page). Another alternative is to pass the backup folder directly to the Restore Custom Action, similarly to how it is currently passed to the Backup Custom Action.

3. In the Custom Actions page, a "Property Set with Formatted" Custom Action is used to set the value of the BackupList Property (used by the Backup script).

4. Note how the Backup Custom Action has been configured. The backup folder as well as the backup list are specified through the "Action Data" field in the format specified above. Note that the same backup folder that is written to the registry must be used here also. Note also the Execution Condition and that the CA is set as "Deferred with no impersonation" as discussed above.

5. The Restore Custom Action is similarly set as "Deferred with no impersonation". Note the Execution Condition and the value of the "Action Data" field (which specifies the value of the CustomActionData Property):

Code: Select all

[BACKUP_FOLDER]|[BackupList]<>[RestoreLocationList]
By specifying the backup folder here directly ("[CommonAppDataFolder][ProductName]" instead of "[BACKUP_FOLDER]"), you can avoid writing the backup folder to the registry and then using a Search to retrieve it.

Hope this helps.

Regards,
Ionut
Attachments
BackupRestore-modified.zip
(64.74 KiB) Downloaded 744 times
Denis Toma
Advanced Installer Team
http://www.advancedinstaller.com/
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: How to backup and restore files/folders in APPDIR?

Thank you very much. It's a very good script.
' Delete the temporary folder
WriteToLog("[Restore]: Deleting temp folder " & Chr(34) & tfolder & Chr(34))
fso.DeleteFolder(RemoveTrailingBackslash(tfolder))
WriteToLog("[Restore]: Done.")
I have been tested. After the above code is changed as the following code, will not prompt an error in installation process. If the backup folder does not exist.
' Delete the temporary folder
If (fso.FolderExists(tfolder)) Then
WriteToLog("[Restore]: Deleting temp folder " & Chr(34) & tfolder & Chr(34))
fso.DeleteFolder(RemoveTrailingBackslash(tfolder))
WriteToLog("[Restore]: Done.")
End If
Good mood every day!
Zhang Shangyi
Ionut
Posts: 605
Joined: Tue Nov 22, 2005 11:29 am
Contact: Website

Re: How to backup and restore files/folders in APPDIR?

Hi Zhang Shangyi,
After the above code is changed as the following code, will not prompt an error in installation process. If the backup folder does not exist.
Good point. I have included this check in the Restore script.

Regards,
Ionut
Denis Toma
Advanced Installer Team
http://www.advancedinstaller.com/
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: How to backup and restore files/folders in APPDIR?

Hello, Ionut

T have been tested again. The following issues be found:

1. If Action Data in Backup Custom Action is [TempFolder]<>[BackupList], and Action Data in Restore Custom Action is [TempFolder]|[BackupList]<>[RestoreLocationList], a error that can not to delete will be prompted at Restore Custom Action. The reason may be that other folders or files contains under the [TempFolder] folder is using. If change the [TempFolder] to other folder contains other folders or files, this folder and subfolders or files will be deleted in its entirety.

2. To do operation same as the previous script, For example, The Action Data in Backup Custom Action is NULL, and Action Data in Restore Custom Action is [tfolder|[BackupList]<>[RestoreLocationList] in the same installation process (In this case, there should be no need to write [tfolder] property into the registry and to search it), a error same as above that can not to delete will be prompted at Restore Custom Action.

3. In the same installation process, if Action Data in Backup Custom Action is BackupFolder<>[BackupList], and Action Data in Restore Custom Action is [tfolder]|[BackupList]<>[RestoreLocationList], will also be an error. [tfolder] property value not been geted from BackupFolder.

Note,
If Not Session.Mode(msiRunModeScheduled) Then
' This Property will be used by the Restore script
Session.Property("tfolder") = tfolder
WriteToLog("[Backup]: Setting tfolder Property to: " & Chr(34) & tfolder & Chr(34))
WriteToLog("[Backup]: Done.")
End If
With regard to the above two and three issues, both TemporaryFolder and BackupFolder can be passed to [tfolder] in any one of two Execution Options, if change the above code to the following code.
' This Property will be used by the Restore script
Session.Property("tfolder") = tfolder
WriteToLog("[Backup]: Setting tfolder Property to: " & Chr(34) & tfolder & Chr(34))
WriteToLog("[Backup]: Done.")
Zhanks,
Zhang Shangyi
Ionut
Posts: 605
Joined: Tue Nov 22, 2005 11:29 am
Contact: Website

Re: How to backup and restore files/folders in APPDIR?

Hi Hi Zhang Shangyi,
1. If Action Data in Backup Custom Action is [TempFolder]<>[BackupList], and Action Data in Restore Custom Action is [TempFolder]|[BackupList]<>[RestoreLocationList], a error that can not to delete will be prompted at Restore Custom Action. The reason may be that other folders or files contains under the [TempFolder] folder is using. If change the [TempFolder] to other folder contains other folders or files, this folder and subfolders or files will be deleted in its entirety.
You should use a subfolder of the "TempFolder" as the backup directory, not the "TempFolder" itself. That is, you can use for instance "[TempFolder]MyBackupFolder" instead of "[TempFolder]". The whole "TempFolder" cannot be deleted because it contains temporary files that are held open by programs or by the OS.
2. To do operation same as the previous script, For example, The Action Data in Backup Custom Action is NULL, and Action Data in Restore Custom Action is [tfolder|[BackupList]<>[RestoreLocationList] in the same installation process (In this case, there should be no need to write [tfolder] property into the registry and to search it), a error same as above that can not to delete will be prompted at Restore Custom Action.

With regard to the above two and three issues, both TemporaryFolder and BackupFolder can be passed to [tfolder] in any one of two Execution Options, if change the above code to the following code.
' This Property will be used by the Restore script
Session.Property("tfolder") = tfolder
WriteToLog("[Backup]: Setting tfolder Property to: " & Chr(34) & tfolder & Chr(34))
WriteToLog("[Backup]: Done.")
Note that the Backup Custom Action must be set as "Immediate", not "Deferred" in order to set the "tfolder" Property. This is because "Deferred" execution Custom Actions cannot set Property values, they can only receive data through the CustomActionData Property. An attempt to set the value of a Property from a "Deferred" Custom Action will fail.

Regards,
Ionut
Denis Toma
Advanced Installer Team
http://www.advancedinstaller.com/
z_shangyi
Posts: 27
Joined: Sat Aug 26, 2006 5:44 am

Re: How to backup and restore files/folders in APPDIR?

Thank you very much!

I tried to use the wildcard *.* copying files with more than one Custom Actions (because I want to copy files under many different sources folders to many different target folders, but I do not know these file name), but failed. I would like to ask how to modify this script?
If fso.FileExists(path) Then
fso.CopyFile path, tfolder
WriteToLog("[Backup]: File " & Chr(34) & path & Chr(34) &_
" copied to " & Chr(34) & tfolder & Chr(34))
End If
I changed it to like this below, I do not know right?
If fso.FileExists(path) Then
fso.CopyFile path, tfolder
WriteToLog("[Backup]: File " & Chr(34) & path & Chr(34) &_
" copied to " & Chr(34) & tfolder & Chr(34))
Else
newpath = FileTrailingBackslash(path) & "\"
If fso.FolderExists(newpath) Then
fso.CopyFile path, tfolder
WriteToLog("[Backup]: File " & Chr(34) & path & Chr(34) &_
" copied to " & Chr(34) & tfolder & Chr(34))
End If
End If

If fso.FolderExists(path) Then
fso.CopyFolder FolderTrailingBackslash(path), tfolder
WriteToLog("[Backup]: Folder " & Chr(34) & path & Chr(34) &_
" copied to " & Chr(34) & tfolder & Chr(34))
Else
newpath = FolderTrailingBackslash(path) & "\"
If fso.FolderExists(newpath) Then
fso.CopyFolder path, tfolder
WriteToLog("[Backup]: Folder " & Chr(34) & path & Chr(34) &_
" copied to " & Chr(34) & tfolder & Chr(34))
End If
End If
' -----------------------------------------------------------------------------
' @info Removes the trailing backslash \ (if there is one)
' -----------------------------------------------------------------------------
Function FileTrailingBackslash(folderPath)
If Right(folderPath, 4) = "\*.*" Then
FileTrailingBackslash = Left(folderPath, Len(folderPath) - 4)
Else
FileTrailingBackslash = folderPath
End If
End Function


' -----------------------------------------------------------------------------
' @info Removes the trailing backslash \ (if there is one)
' -----------------------------------------------------------------------------
Function FolderTrailingBackslash(folderPath)
If Right(folderPath, 1) = "\" Then
FolderTrailingBackslash = Left(folderPath, Len(folderPath) - 1)
ElseIf Right(folderPath, 2) = "\*" Then
FolderTrailingBackslash = Left(folderPath, Len(folderPath) - 2)
Else
FolderTrailingBackslash = folderPath
End If
End Function
And can only use a Custom Action to solve it? That is, in backupFolder attributes can be defined in many different target folders.

Happy every day!
Zhang Shangyi

Return to “Common Problems”