Dava
Posts: 11
Joined: Thu Oct 10, 2019 11:11 am

Remove entire folder content except subfolder

I am in the situation I have a parent directory and a subdirectory.
My wish is to delete all files inside the parent directory but mantain the subdirecotry.
I can't do it with uninstall cleanup and operations on removing folders.
Is there any option or workaround to accomplish this task?

Thanks.
Catalin
Posts: 7513
Joined: Wed Jun 13, 2018 7:49 am

Re: Remove entire folder content except subfolder

Hello,

Is the folder created on the disk by your package or is the folder already on the machine? Also, when you want to perform this operation, during the install of your package or during an uninstall?

For instance, if the folder is already on the disk and you want to remove it during the installation of your package, you can use a script (e.g. PowerShell, VBScript, etc) that can search for it and remove its content. For instance, if we have the following folder on the machine:

C:\
--->MyFolder
------>MySubFolder
------>someFile.txt

a simple PowerShell script which will delete the content of the folder, except the subfolder would be:

Code: Select all

Get-ChildItem C:\MyFolder -Exclude MySubFolder | Remove-Item -Recurse -Force
Basically, the Get-ChildItem will get the content of the "MyFolder" folder:
Get-ChildItem.png
Get-ChildItem.png (31.04 KiB) Viewed 5442 times
The "-Exclude" parameter is used to exclude our folder from it:
Get-ChildItemExclude.png
Get-ChildItemExclude.png (8.04 KiB) Viewed 5442 times
The PowerShell script can be added to your project as a "Run Inline PowerShell script" custom action. It should be scheduled during the "Remove Resources" action group, with its execution time being of "When the system is being modified (deferred)".

The above scenario is mostly intended for when you need to do the deletion during install (when the folder is already present on the disk).

If you want to to this during the uninstall (to remove a folder that was created by your package), please let me know as we may have to do some more modifications.

Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Dava
Posts: 11
Joined: Thu Oct 10, 2019 11:11 am

Re: Remove entire folder content except subfolder

Hi,
The folder I don't want to remove is created by the package.

I think Advanced Installer already have this feature, take some tests to confirm.
Try to mark the subfolder that you don't want to remove with the "Do not overwrite existing files" in the operation tab. Maybe the removal of the file is seen by AI as a replacement, so not avoiding the overwrite mark this folder as non-removable.
Catalin
Posts: 7513
Joined: Wed Jun 13, 2018 7:49 am

Re: Remove entire folder content except subfolder

Hello,

Thank you for your followup on this.

If the folder is created by your package, then you can proceed as it follows:

- go to "Files and Folders" page

- right click on your folder --> "Properties"

- go to "Operations" tab

- select the "Do not overwrite existing files" option and, in the condition field, use the following condition:

Code: Select all

(REMOVE="ALL" AND (NOT UPGRADINGPRODUCTCODE))
This way, the folder will not be removed during a normal uninstall. The above condition can be translated to: the package is removed, but not by an update.

Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”