wixwindows-installerwix3

WiX - How do I copy existing files to a subdirectory before installed files of the same name overwrite them?


I have some file at Program\file.json, and I want my .msi to copy the file to Program\Backup\file.json before installing a new file at Program\file.json so that the old data is not lost.

I tried writing a Program\backup.bat which copies Program\file.json to Program\Backup\file.json and calling it with a CustomAction, but this does not work because I need to call the batch file after it is installed into the system's Program folder during InstallFiles so that it can move the old file. However, InstallFiles is also when the new Program\file.json overwrites the old one.

I also tried doing it the WiX way by putting the following tag in another Component within the same Feature as the File tag responsible for the new Program\file.json:

<CopyFile SourceDirectory="ProgramFolder" DestinationDirectory="BackupFolder" Id="Copy_of_File" SourceName="file.json"/>

("ProgramFolder" is the Id of the Program Directory, and "BackupFolder" is the Id of the Program\Backup Directory)

However, this appears to copy the old version into Program\Backup\file.json but not overwrite it with the new version. I'm not sure why.


Solution

  • This is a bit of a hacky solution and not one that scales well for big regularly-updating projects, but for my purposes it worked.

    Using the CopyFile tag as above while also including <Property Id='REINSTALLMODE' Value='a'/> in the product will force it to reinstall all files, so the issue with it failing to overwrite the old file is averted.